2017-05-02 59 views
-1

我在寫一個簡單的post調用。我的代碼如下:在Express中執行POST查詢時連接ETIMEDOUT錯誤js

var express = require('express');   
var app  = express();     
var bodyParser = require('body-parser'); 
app.use(bodyParser.urlencoded({ extended: true })); 
app.use(bodyParser.json()); 
var port = process.env.PORT || 8080;   
var router = express.Router();    
var mongoose = require('mongoose'); 
mongoose.connect('mongodb://node:[email protected]:27017/Iganiq8o'); 
router.use(function(req, res, next) { 

    console.log('Something is happening.'); 
    next(); 
});enter code here 

router.get('/', function(req, res) { 
    res.json({ message: 'hooray! welcome to our api!' }); 
}); 

var Bear  = require('./app/models/bears'); 
router.route('/bears')  
.post(function(req, res) { 
    var bear = new Bear();  
    bear.name = req.body.name; 
    bear.save(function(err) { 
     if (err) 
      res.send(err); 

     res.json({ message: 'Bear created!' }); 
    }); 

}); 

app.use('/api', router); 
app.listen(port); 
console.log('Magic happens on port ' + port); 

我以前收到以下錯誤

failed to connect to [novus.modulusmongo.net:27017]

我通過更新貓鼬糾正,但現在還是我得到另一個錯誤是:

/node_modules/mongoose/node_modules/mongodb/lib/server.js:242 
    process.nextTick(function() { throw err; }) 
           ^

錯誤:連接ETIMEDOUT 54.209.84.209:27017

+1

是蒙戈服務器上運行?在啓動連接之前,服務應該正在運行。 –

+0

您在代碼中給出的URL看起來像一個遠程數據庫。你確定你有權訪問它並且正在運行?嘗試在本地運行數據庫。 –

+0

是的,我已經事先啓動了mongo服務器 –

回答

0

試着用下面的例子

var mg= require('mongoose'); 
var db = mg.connection; 
mg.connect('mongodb://node:[email protected]:27017/Iganiq8o'); 

db.on('open', function (ref) { 
    console.log('open connection to mongo server.'); 
}); 

db.on('connected', function (ref) { 
    console.log('Connected connection to mongo server.'); 
}); 

db.on('disconnected', function (ref) { 
    console.log(ref); 
    console.log('disconnected connection.'); 
}); 

db.on('close', function (ref) { 
    console.log('close connection.'); 
}); 

db.on('error', function (ref) { 
    console.log(ref); 
    console.log('Error connection.'); 
}); 

db.on('reconnected', function() { 
    console.log('MongoDB reconnected!'); 
}); 

下面是我得到的錯誤。

{ MongoError: failed to connect to server [novus.modulusmongo.net:27017] on first connect 
at Pool.<anonymous> (E:\v2.4.1\node_modules\mongoose\node_modules\mongodb-core\lib\topologies\server.js:313:35) 
at emitOne (events.js:96:13) 
at Pool.emit (events.js:188:7) 
at Connection.<anonymous> (E:\v2.4.1\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:260:12) 
at Connection.g (events.js:291:16) 
at emitTwo (events.js:106:13) 
at Connection.emit (events.js:191:7) 
at Socket.<anonymous> (E:\v2.4.1\node_modules\mongoose\node_modules\mongodb-core\lib\connection\connection.js:162:49) 
at Socket.g (events.js:291:16) 
at emitOne (events.js:96:13) 
at Socket.emit (events.js:188:7) 
at emitErrorNT (net.js:1278:8) 
at _combinedTickCallback (internal/process/next_tick.js:74:11) 
at process._tickCallback (internal/process/next_tick.js:98:9) 
name: 'MongoError', 
message: 'failed to connect to server [novus.modulusmongo.net:27017] on first connect' } 

所以我覺得它只是連接字符串或端口或用戶名或密碼是不正確的。嘗試通過簡單地改變連接字符串,端口,用戶名或密碼與上面的代碼連接..

記住27017是不是默認端口始終專門託管服務器上

嘗試MLAB,看看它是否連接了免費測試並得到您的連接字符串這裏https://mlab.com/,看看是否所有代碼都可以正常使用

希望幫助你