我正在尝试通过NodeJS webapp连接到mLab MongoDB数据库,主要代码如下:
var express= require('express');
var app=express();
var mongoose=require('mongoose');
var config=require('./config');
var apiMain=require('./controllers/apiMain');
var timeout = require('connect-timeout');
app.use(timeout('10s'));
var port=process.env.PORT || 3000;
app.use('/', express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
mongoose.connect(config.getDbConnString(), { useMongoClient: true });
mongoose.Promise=global.Promise;
var db=mongoose.connection;
db.on('error', console.error.bind(console,'errore di connessione'));
apiMain(app);
app.listen(port);
连接字符串(通过配置文件获取)是正确的,我得到没有编译错误。
顺便说一下,每当我尝试使用 nodemon app 运行它时,我都会收到此错误
errore di connessione { Error: read ETIMEDOUT
at _errnoException (util.js:1022:11)
at TCP.onread (net.js:615:25) name: 'MongoError', message: 'read ETIMEDOUT' }
Unhandled rejection Error: read ETIMEDOUT
at _errnoException (util.js:1022:11)
at TCP.onread (net.js:615:25)
我支持将超时时间增加到10秒以上:当然,它也不起作用。
答案 0 :(得分:0)
在尝试了不同的解决方案之后(比如在项目中重新安装整个npm),我摆脱了问题。
节点,没有提供有关锁定端口的任何信息,而且问题:我试图通过其他连接来解决这个问题。
我的电脑(Mac),没有任何防火墙,也没有锁定端口:无线公司拥有它。
我得到了这个解决方案,试图通过CLI ping mlab服务器。