node.js - digitalocean ubuntu 18.4 上的 Mongodb / nodejs 问题
问题描述
我在 digitalocean 上使用 ubuntu 18.4 设置了 droplets,我安装了 nodejs 和 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]
谢谢你。
解决方案
推荐阅读
- c++ - c++ libssh - ssh_channel_read() 不断返回 0 并且 ssh_channel_is_open() 返回 false
- java - 当 webview 水平滚动时禁用滚动视图垂直滚动:android
- c# - 将日期/时间转换为字符串时出错
- python - 在python中返回函数的值
- java - 捕获字符串中属性的值?
- docker - Docker(docker-compose.yml 文件)定义网络
- php - 如何使用 codeigniter 在邮件中设置动态标题?
- hyperledger-fabric - Hyperledger Fabric 中的对等通道创建失败
- react-native - 监视 this.state 以外的对象/字符串
- c++ - 具有多个可执行文件和依赖项的 Makefile