PM2 在 NodeJS (Heroku) 上不起作用

Soc*_*ife 5 heroku node.js express pm2

我正在使用 PM2 让 NodeJS (Express) 连续运行。在我的本地机器上,以下在我的packages.json 中有效:

"scripts": {
    "start": "pm2 start ./bin/www"
}
Run Code Online (Sandbox Code Playgroud)

之后,我使用“npm start”来运行我的应用程序。这一切正常。

现在我想将我的应用程序部署到 Heroku。我添加了一个带有 'web: npm start' 的 Procfile。之后,我将我的 packages.json 稍微更改为:

"scripts": {
    "preinstall": "npm install pm2 -g",
    "start": "pm2 start ./bin/www"
 }
Run Code Online (Sandbox Code Playgroud)

但是,我收到此错误:

2018-02-22T19:51:23.861641+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2018-02-22T19:51:23.862201+00:00 heroku[web.1]: Stopping process with SIGKILL
2018-02-22T19:51:24.007776+00:00 heroku[web.1]: Process exited with status 137
2018-02-22T19:51:24.046849+00:00 heroku[web.1]: State changed from starting to crashed
Run Code Online (Sandbox Code Playgroud)

我已经查找了许多解决此问题的方法,例如将我的 Procfile 更改为 'worker: node app.js' 并运行命令:'$ heroku scale web=0 worker=1'。

但我找不到问题出在哪里。我认为它与“pm2”模块有关,因为当我不使用它时,我的应用程序运行良好。然而,它在短时间内崩溃。希望有人可以帮助我。

“pm2 show www”显示:

? status            ? online                                                       ?
? name              ? www                                                          ?
? restarts          ? 151                                                          ?
? uptime            ? 40s                                                          ?
? script path       ? /Users/user1/Documents/Weview/app/bin/www ?
? script args       ? N/A                                                          ?
? error log path    ? /Users/user1/.pm2/logs/www-error-1.log    ?
? out log path      ? /Users/user1/.pm2/logs/www-out-1.log      ?
? pid path          ? /Users/user1/.pm2/pids/www-1.pid          ?
? interpreter       ? node                                                         ?
? interpreter args  ? N/A                                                          ?
? script id         ? 1                                                            ?
? exec cwd          ? /Users/user1/Documents/Weview/app         ?
? exec mode         ? fork_mode                                                    ?
? node.js version   ? 8.9.1                                                        ?
? watch & reload    ? ?                                                            ?
? unstable restarts ? 0                                                            ?
? created at        ? 2018-02-22T20:22:53.688Z                                     ?
????????????????????????????????????????????????????????????????????????????????????
 Code metrics value 
????????????????????????????
? Loop delay      ? 2.86ms ?
? Active requests ? 0      ?
? Active handles  ? 4  
Run Code Online (Sandbox Code Playgroud)

cod*_*ode 0

用这个

"scripts": {
    "start": "PORT=3000 pm2 start ./bin/www"
}
Run Code Online (Sandbox Code Playgroud)

Heroku 尝试将节点应用程序与 env 变量绑定PORT