连接到节点服务器超时 - 端口已打开

时间:2017-07-23 22:53:46

标签: node.js express

我正在创建一个必须连接到我的Windows计算机的node.js服务器的应用程序......

使用MongoDB,Express和Node.js,我试图使用端口3000访问服务器。我的iphone(或任何其他设备)可以在与运行我的机器相同的网络上连接到节点服务器/ DB服务器。

我尝试访问不同网络(移动设备或不同的wifi)上的服务器时超时。

我尝试将我的快递应用程序绑定到'0.0.0.0',以及当前设置的dhcp IP地址,该地址是运行服务器的机器。

Netstat告诉我端口是开放的。

netstat -an

这是我的index.js代码: 1.我将端口设置在文档的顶部 2.我使用端口3000创建服务器,并在最底部托管“0.0.0.0”。

var http = require('http'), express = require('express'), path = require('path');

MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
CollectionDriver = require('./collectionDriver').CollectionDriver,
FileDriver = require('./fileDriver').FileDriver;

var app = express(); // create Express.js object
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.bodyParser());

var mongoHost = 'localHost'; // CHANGE THESE WHEN ACCESSING A SERVER ON A DIFFERENT MACHINE
var mongoPort = 27017;
var fileDriver;
var collectionDriver;

var mongoClient = new MongoClient(new Server(mongoHost, mongoPort)); 
mongoClient.open(function(err, mongoClient) { 
  if (!mongoClient) {
      console.error("Error! Exiting... Must start MongoDB first");
      process.exit(1); 
  }
  var db = mongoClient.db("MyDatabase");
  fileDriver = new FileDriver(db);
  collectionDriver = new CollectionDriver(db); 
});



app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function (req, res) {
  res.send('<html><body><h1>Hello World</h1></body></html>');
});

// For saving session rail/prize pictures
app.post('/files', function(req,res) {fileDriver.handleUploadRequest(req,res);});
app.get('/files/:id', function(req, res) {fileDriver.handleGet(req,res);});
// For saving Account Profile Pictures
app.post('/accountPictures', function(req,res) {fileDriver.handleUploadRequestAccount(req,res);});
app.get('/accountPictures/:id', function(req, res) {fileDriver.handleGetAccount(req,res);});

// req (request) - The request contains information from the browser:
//                  HTTP agent, information passed in and more
// res (response) -  The response is what we will send back to the user
app.get('/:collection', function(req, res) { 
   var params = req.params; 
   collectionDriver.findAll(req.params.collection, function(error, objs) { 
    	  if (error) { res.send(400, error); } 
	      else { 
	          if (req.accepts('html')) { 
    	          res.render('data',{objects: objs, collection: req.params.collection}); 
              } else {
	          res.set('Content-Type','application/json'); 
                  res.send(200, objs); 
              }
         }
   	});
});
 
app.get('/:collection/:entity', function(req, res) {
  console.log("Here was made")
  var params = req.params;
  var entity = params.entity;
  var collection = params.collection;
   if (entity) {
       collectionDriver.get(collection, entity, function(error, objs) { 
          if (error) { res.send(400, error); }
          else { res.send(200, objs); } 
       });
   } else {
      res.send(400, {error: 'bad url', url: req.url});
   }
});

// Add data
app.post('/:collection', function(req, res) { 
    var object = req.body;
    var collection = req.params.collection;
    collectionDriver.save(collection, object, function(err,docs) {
          if (err) { res.send(400, err); } 
          else { 
          	res.send(201, docs); 
          } 
     });
});

// Update data by _id
app.put('/:collection/:entity', function(req, res) { 
    var params = req.params;
    var entity = params.entity; // Name of object retrieved from collection (DB)
    var collection = params.collection; // Table Name (Accounts, JamLocations, etc.)
    if (entity) {
       collectionDriver.update(collection, req.body, entity, function(error, objs) { 
          if (error) { res.send(400, error); }
          else { res.send(200, objs); } 
       });
   } else {
       var error = { "message" : "Cannot PUT a whole collection" };
       res.send(400, error);
   }
});

// Delete data by _id
app.delete('/:collection/:entity', function(req, res) { 
    var params = req.params;
    var entity = params.entity;
    var collection = params.collection;
    if (entity) {
       collectionDriver.delete(collection, entity, function(error, objs) { 
          if (error) { res.send(400, error); }
          else { res.send(200, objs); } //200 b/c includes the original doc
       });
   } else {
       var error = { "message" : "Cannot DELETE a whole collection" };
       res.send(400, error);
   }
});




// Catch all for app.verb, creates a 404 error
app.use(function (req,res) { 
    res.render('404', {url:req.url});
});

// CREATES SERVER
http.createServer(app).listen(app.get('port'), "0.0.0.0", function(){
  console.log('Express server listening on port ' + app.get('host') + " - " + app.get('port'));
});

服务器连接到Mongo很好。

这里我创建了一个入站规则来打开端口3000 ...... inbound rule - port 3000

我尽力在stackoverflow和网络上耗尽所有其他解决方案......如果存在,请指出任何有用的读数!

感谢您的帮助。

0 个答案:

没有答案