javascript PM2 (Node.js) 不监听指定端口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24192971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:20:18  来源:igfitidea点击:

PM2 (Node.js) not listening on specified port

javascriptnode.jspm2

提问by JamesE

I am trying to get a Node/Express application up and running on PM2. I can start the application fine with this command: npm start

我正在尝试在 PM2 上启动并运行 Node/Express 应用程序。我可以使用以下命令正常启动应用程序:npm start

This starts the app fine on port 3000.

这将在端口 3000 上正常启动应用程序。

If I try to start the application with pm2 start app.jsI get the following in the log:

如果我尝试启动应用程序,pm2 start app.js我会在日志中得到以下信息:

{ online: true, success: true, pid: 10714, pm2_version: '0.8.15' }
2014-06-12T19:52:06.789Z : [[[[ PM2/God daemon launched ]]]]
2014-06-12T19:52:06.800Z : RPC interface [READY] on 6666:localhost
2014-06-12T19:52:06.801Z : BUS system [READY] on  6667:localhost
2014-06-12T19:52:06.978Z : Entering in node wrap logic (cluster_mode) for script     /home/user/test/app.js
2014-06-12T19:52:07.115Z : /home/user/test/app.js - id0 worker online

In my bin/www file I have the following specifying the port:

在我的 bin/www 文件中,我指定了以下端口:

app.set('port', process.env.PORT || 3000);

I have also tried running export PORT=3000

我也试过跑步 export PORT=3000

As well as the following in bin/www:

以及 bin/www 中的以下内容:

app.set('port', 3000);

If I run a netstat -an | grep 3000I get nothing back.

如果我运行 anetstat -an | grep 3000我什么也得不到。

回答by JamesE

The answer to this, for anyone using Express, is to run this command:

对于任何使用 Express 的人来说,答案是运行以​​下命令:

pm2 start ./bin/www

pm2 start ./bin/www

I had been running pm2 start app.jswhich did not work.

我一直在运行pm2 start app.js,但没有用。

回答by Peter Lyons

Your app.set('port'...calls are not directly relevant. app.setis just a place to store key/value settings but it provides zero functionality in and of itself. What you want to look at is where you call app.listensince that function is what accepts a port as an argument.

您的app.set('port'...电话没有直接关系。app.set只是一个存储键/值设置的地方,但它本身提供零功能。您要查看的是您调用的位置,app.listen因为该函数接受端口作为参数。

回答by Pavlo Sadovyi

I had a similar problem, with nginx configured as proxy server I couldn't see the Express app running by PM2. When I removed my ~/.pm2folder it worked.

我有一个类似的问题,将 nginx 配置为代理服务器,我看不到 PM2 运行的 Express 应用程序。当我删除我的~/.pm2文件夹时,它起作用了。

回答by Дмитрий И

I use this

我用这个

pm2.json

pm2.json

[
{
  "exec_mode": "fork_mode",
  "cwd" : "/opt/acme_service",
  "script": "acme_service.js",
  "name": "acme_service",
  "restart_delay":"9000",
  "port"       : 8081,
  "node_args": [ "--acme" ],
  "error_file": "/var/log/acme_service.err.log",
  "out_file": "/var/log/acme_service.out.log"
}
]

"port" : 8081 - accept port connection. same in app

“端口”:8081 - 接受端口连接。在应用程序中相同

var server = app.listen(8081 , '0.0.0.0');