"Request timeout" 在 Heroku 上

"Request timeout" on Heroku

我在 Heroku 上部署网络应用程序时遇到问题。我已经在 MongoDB Atlas 上设置了数据库。当我在本地 运行ning "node app.js" 时,数据库正在工作。在 Heroku 上部署我的项目后,出现应用程序错误,当我 运行 "heroku logs --tail" 时出现以下错误;

2020-03-07T19:53:58.266177+00:00 heroku[web.1]: Idling
2020-03-07T19:53:58.269259+00:00 heroku[web.1]: State changed from up to down
2020-03-07T19:53:59.799552+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2020-03-07T19:53:59.987938+00:00 heroku[web.1]: Process exited with status 143
2020-03-07T20:02:06.452667+00:00 heroku[web.1]: Unidling
2020-03-07T20:02:06.468513+00:00 heroku[web.1]: State changed from down to starting
2020-03-07T20:02:09.189010+00:00 heroku[web.1]: Starting process with command `node app.js`
2020-03-07T20:02:11.522634+00:00 app[web.1]: Server started on port 3000
2020-03-07T20:02:12.987457+00:00 heroku[web.1]: State changed from starting to up
2020-03-07T20:02:41.597213+00:00 app[web.1]: (node:4) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connection <monitor> to 35.172.242.193:27017 closed
2020-03-07T20:02:41.597235+00:00 app[web.1]: at new MongooseServerSelectionError (/app/node_modules/mongoose/lib/error/serverSelection.js:22:11)
2020-03-07T20:02:41.597236+00:00 app[web.1]: at NativeConnection.Connection.openUri (/app/node_modules/mongoose/lib/connection.js:808:32)
2020-03-07T20:02:41.597237+00:00 app[web.1]: at Mongoose.connect (/app/node_modules/mongoose/lib/index.js:333:15)
2020-03-07T20:02:41.597237+00:00 app[web.1]: at Object.<anonymous> (/app/app.js:14:10)
2020-03-07T20:02:41.597238+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:955:30)
2020-03-07T20:02:41.597238+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
2020-03-07T20:02:41.597239+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:811:32)
2020-03-07T20:02:41.597239+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:723:14)
2020-03-07T20:02:41.597239+00:00 app[web.1]: at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
2020-03-07T20:02:41.597240+00:00 app[web.1]: at internal/main/run_main_module.js:17:11
2020-03-07T20:02:41.597341+00:00 app[web.1]: (node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
2020-03-07T20:02:41.597434+00:00 app[web.1]: (node:4) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
2020-03-07T20:02:43.524590+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/" host=protected-coast-44428.herokuapp.com request_id=9a7b66e6-1280-4d0c-b13d-10548513bc70 fwd="159.146.14.191" dyno=web.1 connect=0ms service=30000ms status=503 bytes=0 protocol=https
2020-03-07T20:03:17.579588+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=GET path="/favicon.ico" host=protected-coast-44428.herokuapp.com request_id=3d335054-de74-4abb-982c-135988919edb fwd="159.146.14.191" dyno=web.1 connect=1ms service=30000ms status=503 bytes=0 protocol=https

请注意,我的项目中没有任何 favicon.ico。我在我以前的项目中有它,由于这个错误,我在 Heroku 上删除了那个项目,我不知道为什么我仍然在我的错误中得到 favicon。

我确保我在app.js中输入了正确的用户名和密码并且我也将我的IP添加到白名单中

mongoose.connect("mongodb+srv://username:password*@cluster0-h6uqp.mongodb.net/todolistDB",
{ useNewUrlParser: true, useUnifiedTopology: true });

免费计划的 Heroku,在 30 分钟不活动后关闭测功机。 More on this here.

根据您的日志,mongo 连接似乎在应用程序启动 30 分钟后关闭。看起来你受此影响。

该错误似乎是由于未处理应用程序中关闭的 mongo 连接引起的。

我通过在 MongoDB 上更改对 'ALLOW ACCESS FROM ANYWHERE' 的网络访问解决了这个问题。 Here