node.js - 运行 express 应用程序时无法连接到 mongodb
问题描述
我是编程新手,目前正在学习 MEAN。基本上我在尝试从家里运行 nodemon 服务器时收到以下错误消息。当我在学校或当地的星巴克时,不会发生此问题。有人可以看看以下错误我得到MongoNetworkError: getaddrinfo ENOTFOUND localhost localhost:27017并帮助我理解为什么我无法连接到 mongoDB?为了澄清起见,是的,我确实在单独的终端上运行了 mongod。我在 2011 macbook pro 上运行 macOS High Sierra -v 10.13.4。请帮我。谢谢你。
这是我在启动服务器时遇到的错误:
[nodemon] 开始
node server
(node:27636) DeprecationWarning:当前的 URL 字符串解析器已被弃用,并将在未来的版本中删除。要使用新的解析器,请将选项 { useNewUrlParser: true } 传递给 MongoClient.connect。
监听 8000 端口
(node:27636) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND localhost localhost:27017] at Pool。
(/Users/ROBONYX21/Desktop/Angular/product_list/node_modules/mongodb-core/lib/topologies/server.js:564:11)
在连接的 Pool.emit (events.js:182:13)。>(/Users/ROBONYX21/Desktop/Angular/product_list/node_modules/mongodb-core/lib/connection/pool.js:317:12) 在 Object.onceWrapper (events.js:273:13) 在 Connection.emit (events .js:182:13) 在套接字。>(/Users/ROBONYX21/Desktop/Angular/product_list/node_modules/mongodb-core/lib/connection/connection.js:246:50) 在 Object.onceWrapper (events.js:273:13) 在 Socket.emit (events .js:182:13) 在emitErrorNT (internal/streams/destroy.js:82:8) 在emitErrorAndCloseNT (internal/streams/destroy.js:50:3) 在process._tickCallback (internal/process/next_tick.js: 63:19)
(节点:27636)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。(拒绝编号:1)
(节点:27636)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。
这是运行 mongod mongod port 27017的终端上显示的内容
2018-11-22T09:48:15.268-0800 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2018-11-22T09:48:20.581-0800 I CONTROL [initandlisten]
2018-11-22T09:48:20.581-0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-11-22T09:48:20.581-0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-11-22T09:48:20.581-0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-11-22T09:48:20.581-0800 I CONTROL [initandlisten]
2018-11-22T09:48:20.581-0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2018-11-22T09:48:20.581-0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2018-11-22T09:48:20.581-0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2018-11-22T09:48:20.581-0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2018-11-22T09:48:20.581-0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2018-11-22T09:48:20.581-0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2018-11-22T09:48:20.581-0800 I CONTROL [initandlisten]
2018-11-22T09:48:20.581-0800 I CONTROL [initandlisten]
2018-11-22T09:48:20.581-0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2018-11-22T09:48:22.917-0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2018-11-22T09:48:23.545-0800 I NETWORK [initandlisten] waiting for connections on port 27017
这是我连接到猫鼬数据库的 node.js 文件。 猫鼬.js
解决方案
推荐阅读
- fortran - 如何在fortran中生成[0,180]范围内的整数随机数?
- sql - 如何将来自 json 对象的数组与普通数组进行比较?
- javascript - 带有 D3JS 的 React Native 的 StreamGraph。需要将所有区域合并为一个区域。迭代不起作用
- docker - 如何创建调用 docker 映像的 VSCode 任务,同时设置为在 .md 文件上发生文件更改时运行?
- python - 在 Python 中表示直方图 x 轴内的区间
- c# - CosmosDB 更改更改源处理器名称
- javascript - 在 html5 画布中创建形状时创建了太多对象
- php - ci 休息服务器允许 CORS 不起作用
- angularjs - 无法设置未定义或空引用的属性“属性名称”
- autohotkey - 使用 Enter 按钮提交,而在其他地方正常工作