我正在尝试为mongo数据库创建一个简单的rest api
我有一个server.js
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-Parser');
const app = express();
app.use(bodyParser.json());
const db = require('./config/keys').mongoURI;
mongoose
.connect('mongodb://test:test123@ds241012.mlab.com:41012/test_db' ,{useNewUrlParser: true})
.then(() => console.log('DB Connected'))
.catch(err => console.log(err));
const port = process.env.PORT || 4000;
app.listen(port, () => console.log('Server started'));
我使用nodemon,当我执行npm run server时,我在终端(console.log)中看到“服务器已启动”
此后,我得到以下输出端子
{ MongoNetworkError: failed to connect to server [ds241012.mlab.com:41012] on first connect [MongoNetworkError: connection 0 to ds241012.mlab.com:41012 timed out]
at Pool.<anonymous> (/Users/cdd/Documents/_Work/cd/React/test_db/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (/Users/cdd/Documents/_Work/cd/React/test_db/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (/Users/cdd/Documents/_Work/cd/React/test_db/node_modules/mongodb-core/lib/connection/connection.js:257:10)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:106:13)
at Socket.emit (events.js:208:7)
at Socket._onTimeout (net.js:420:8)
at ontimeout (timers.js:482:11)
at tryOnTimeout (timers.js:317:5)
at Timer.listOnTimeout (timers.js:277:5)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
如果我随后更新server.js,终端将返回到“服务器已启动”
此输出是否正常
答案 0 :(得分:0)
如果您已经将尝试连接mongodb的计算机的IP地址列入了白名单,那么出现错误的唯一可能性就是防火墙阻止了它。