【问题标题】:Connection Time out in Node JsNode Js中的连接超时
【发布时间】:2016-06-01 03:42:51
【问题描述】:

我已经在我的 Ubuntu 服务器中安装了 Node Js(v6.1.0) 和 MongoDB。

在 package.json 中,我有以下代码,

package.json:
{
"name":"restful",
"main":"server.js",
"dependencies":{
"express":"latest",
"mongoose":"latest",
"node-restful":"latest"
}
}

在 server.js 中,我有以下代码,

var express=require('express'),
    restful=require('node-restful'),
    mongoose=restful.mongoose;

 var app=express();

  var bodyParser = require('body-parser');
  app.use(bodyParser());
  var methodOverride = require('method-override')

// override with the X-HTTP-Method-Override header in the request 
app.use(methodOverride('X-HTTP-Method-Override'));

 mongoose.connect('mongodb://localhost/restful');
 var ProductSchema=mongoose.Schema({
  name:String,
  sku:String,
  price:Number
 });
 var Products=restful.model('products',ProductSchema);
 Products.methods(['get','put','post','delete']);
 Products.register(app,'/api/products');

 app.listen(3000);
 console.log('Server is running at port 3000');

当我在终端运行代码节点 server.js 时,它说 “服务器正在端口 3000 运行”。当我在浏览器中尝试说, http://example.com:3000/api/products,

它显示“无法访问站点。连接超时”

但上面的代码在 localhost 中运行良好。在服务器中它不工作。

我也安装了 npm。

【问题讨论】:

    标签: javascript node.js mongodb


    【解决方案1】:

    请确保您在本地主机上工作,所以请访问

    http://localhost:3000/api/products 或 127.0.0.1:3000/api/products 如果您在某个实时服务器上工作,则输入正确的域地址/IP 地址

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      • 1970-01-01
      • 2021-05-15
      • 2018-09-18
      • 1970-01-01
      相关资源
      最近更新 更多