我有一个简单的NodeJS项目,应该连接到MongoDB服务器:
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const bodyParser = require('body-parser');
const db = require('./config/db');
const app = express();
const port = 8030;
app.use(bodyParser.urlencoded({ extended: true }));
console.log(db.url)
MongoClient.connect(db.url, (err, database) => {
if(err) return console.log(err)
require('./app/routes')(app, database);
app.listen(port, () => {
console.log('Server is listenning on port 8030....');
});
});
变量db.url
包含
mongodb://test:test@ds147421.mlab.com:47421/testnode
其中test
是数据库用户的用户名,test
是他的密码。
我抛出了这个错误:
{ [MongoError: failed to connect to server [ds147421.mlab.com:47421] on first connect [MongoError: connection 0 to ds147421.mlab.com:47421 timed out]]
name: 'MongoError',
message: 'failed to connect to server [ds147421.mlab.com:47421] on first connect [MongoError: connection 0 to ds147421.mlab.com:47421 timed out]' }
我不知道我在这里做错了什么。
有什么想法吗?