初学者使用 Node.js 我在 Heroku 上部署服务器时遇到问题。
\n\n我制作了一个使用 mongodb 的 server.js 应用程序。\n当我使用“npm start”cmd 在本地( http://localhost:3000 )上运行服务器时,它工作正常,我能够连接到我的mongo数据库通过命令:
\n\nmongoose.connect(\n\'mongodb+srv://yolanpibrac:mypassword@cluster0-7z1th.mongodb.net/test? retryWrites=true&w=majority\';, \n{ useNewUrlParser: true }) \n
Run Code Online (Sandbox Code Playgroud)\n\n(我遵循了本教程:[ https://appdividend.com/2018/04/14/how-to-deploy-nodejs-app-to-heroku/])
\n\n但是当我在heroku上部署我的应用程序时,该应用程序无法连接到mongo db。我有以下错误(通过检查:heroku log -t):
\n\n2019-08-06T21:06:33.086146+00:00 heroku[web.1]: Starting process with command `node src/server.js`\n2019-08-06T21:06:35.404641+00:00 heroku[web.1]: State changed from starting to up\n2019-08-06T21:06:35.058357+00:00 app[web.1]: Listening on port 26845\n2019-08-06T21:06:36.144056+00:00 app[web.1]: Error while DB connecting\n2019-08-06T21:06:36.149809+00:00 app[web.1]: { MongoNetworkError: failed to connect to server [cluster0-shard-00-00-7z1th.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to cluster0-shard-00-00- 7z1th.mongodb.net:27017 closed]\n2019-08-06T21:06:36.149812+00:00 app[web.1]: at Pool.<anonymous> …
Run Code Online (Sandbox Code Playgroud)