首页 > 解决方案 > 运行 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

标签: node.jsmongodbexpress

解决方案


推荐阅读