PM2 (Node.js) not listening on specified port
Asked Answered
R

5

20

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

This starts the app fine on port 3000.

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

{ 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:

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

I have also tried running export PORT=3000

As well as the following in bin/www:

app.set('port', 3000);

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

Radtke answered 12/6, 2014 at 19:58 Comment(3)
Well, you're setting it to process.env.PORT unless that's undefined, then you'd be setting it to 3000. Did you try just 3000 ?Pb
Yes, I tried that as well - it is still not listening on port 3000.Radtke
That log output looks like what's from pm2.log, is there a log file for your application? Perhaps within it you console.log something after the server starts with the host/port?Infundibuliform
R
50

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

pm2 start ./bin/www

I had been running pm2 start app.js which did not work.

Radtke answered 27/6, 2014 at 19:5 Comment(7)
And what is the difference here?Atencio
You saved me 2 days. It would be great if you can give an explanation.Indoiranian
Worked for me. Great. I am unable to find the correct answer anywhere else. pm2 start ./bin/wwwCertificate
works for me...thank you very much!!! it was not working at 3000 eitherWelloff
This makes absolutely no sense. How would ./bin/www have any idea where my sever.js file is?Allow
@SephReed, i hear you. here's how i made it work (i.e. pi@myserver:~/nodeapps/expserver $ pm2 start). if you had pm2 generate a file ecosystem.config.js in the project folder (the same folder in which you have package.json), change the line script: 'app.js' to script: './bin/www'. That'll hopefully retain all the other parameters in the ecosystem configuration file.Brame
my hero........Bosk
L
3

Your app.set('port'... calls are not directly relevant. app.set is 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.listen since that function is what accepts a port as an argument.

Langham answered 12/6, 2014 at 23:55 Comment(1)
It looks like it is still set in bin/www - var server = app.listen(app.get('port'), function() { debug('Express server listening on port ' + server.address().port); });Radtke
L
1

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 ~/.pm2 folder it worked.

Labionasal answered 24/8, 2016 at 13:59 Comment(0)
D
0

I use this

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

var server = app.listen(8081 , '0.0.0.0');
Douty answered 30/1, 2020 at 14:33 Comment(1)
Please explain how and why do you consider this to be a solution.Fortuitism
M
0

In my case it was listening in a specific port, but for some reason my interface could not find the host / port, then I installed NGINX and set the default, on /etc/nginx/sites-enabled/, to:

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        server_name _;

        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection “upgrade”;
            proxy_max_temp_file_size 0;
            proxy_pass ip:port/; <<<<<<<<< Change the IP and port >>>>>>>>>
            proxy_redirect off;
            proxy_read_timeout 240s;
        }

}

Check the last lines. The reverse proxy worked.

Obs: The /etc/nginx/sites-enabled/default is included on /etc/nginx/nginx.conf, which is then used as the configuration for NGINX

Marvamarve answered 28/8, 2020 at 19:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.