我在digitalocean上使用ubuntu 18.4设置了Droplet,在其中安装了Node.js和mongodb。
当我尝试通过nodejs连接到我的API时,在执行pm2日志时会收到此错误:
0|index | App is running at http://localhost:5000 in development mode
0|index | test Press CTRL-C to stop
0|index |
0|index | You have triggered an unhandledRejection, you may have
forgotten to catch a Promise rejection:
0|index | App is running at http://localhost:5000 in development mode
0|index | test Press CTRL-C to stop
0|index |
0|index | You have triggered an unhandledRejection, you may have
forgotten to catch a Promise rejection:
0|index | App is running at http://localhost:5000 in development mode
0|index | test Press CTRL-C to stop
0|index |
0|index | You have triggered an unhandledRejection, you may have
forgotten to catch a Promise rejection:
/root/.pm2/logs/index-error.log last 15 lines:
0|index | at deprecated (internal/util.js:47:15)
0|index | at connect
(/var/www/xxx/xxx/node_modules/mongodb/lib/operations/mongo_client_ops.js:179:3)
0|index | at connectOp (/var/www/xxx/xxx/node_modules/mongodb/lib/operations/mongo_client_ops.js:283:3)
0|index | at executeOperation (/var/www/xxx/xxx/node_modules/mongodb/lib/utils.js:420:24)
0|index | at MongoClient.connect (/var/www/xxx/xxx/node_modules/mongodb/lib/mongo_client.js:168:10)
0|index | at Promise (/var/www/xxx/xxx/node_modules/mongoose/lib/connection.js:493:12)
0|index | at new Promise (<anonymous>)
0|index | at NativeConnection.Connection.openUri (/var/www/xxx/xxx/node_modules/mongoose/lib/connection.js:490:19)
0|index | at Mongoose.createConnection (/var/www/xxx/xxx/node_modules/mongoose/lib/index.js:190:17)
0|index | at Object.<anonymous> (/var/www/xxx/xxx/index.js:6:30)
0|index | at Module._compile (module.js:652:30)
0|index | at Object.Module._extensions..js (module.js:663:10)
0|index | at Module.load (module.js:565:32)
0|index | at tryModuleLoad (module.js:505:12)
0|index | at Function.Module._load (module.js:497:3)
Mongodb运行完美,我的nodejs代码如下:
const express = require('express')
const session = require('express-session');
const bodyParser = require('body-parser');
const path = require('path')
const mongoose = require('mongoose');
const connection = mongoose.createConnection(process.env.MONGODB_URI);
const app = (module.exports = express());
mongoose.connect("mongodb://localhost:27017/Lab", function (err) {
if (err) console.error('Could not connect to mongodb.');
});
...
app.listen(app.get('port'), () => {
console.log(
'App is running at http://localhost:%d in %s mode',
app.get('port'),
app.get('env')
);
console.log('test Press CTRL-C to stop\n');
});
在控制台上,我可以看到以下消息:
0|index | App is running at http://localhost:5000 in development mode
0|index | test Press CTRL-C to stop
所以我认为我的nodejs应用运行良好,但与mongodb的连接有问题,有人可以帮我吗?
****更新 当我运行时:在CLI上的mongo我有这个:
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
Server has startup warnings:
2018-08-04T09:56:28.449+0000 I STORAGE [initandlisten]
2018-08-04T09:56:28.449+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-08-04T09:56:28.449+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-08-04T09:56:29.619+0000 I CONTROL [initandlisten]
2018-08-04T09:56:29.619+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-08-04T09:56:29.619+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-08-04T09:56:29.619+0000 I CONTROL [initandlisten]
谢谢。