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.

JamesE
  • 3,833
  • 9
  • 44
  • 82
  • 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` ? – adeneo Jun 12 '14 at 20:00
  • Yes, I tried that as well - it is still not listening on port `3000`. – JamesE Jun 12 '14 at 20:05
  • 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? – dylants Jun 12 '14 at 21:03

5 Answers5

49

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.

JamesE
  • 3,833
  • 9
  • 44
  • 82
  • 10
    And what is the difference here? – astronaut Dec 07 '15 at 12:22
  • You saved me 2 days. It would be great if you can give an explanation. – Fawzan Mar 05 '16 at 13:55
  • Worked for me. Great. I am unable to find the correct answer anywhere else. pm2 start ./bin/www – mythicalcoder Aug 01 '16 at 09:50
  • works for me...thank you very much!!! it was not working at 3000 either – John Sep 28 '17 at 05:04
  • 5
    This makes absolutely no sense. How would ./bin/www have any idea where my sever.js file is? – Seph Reed Nov 26 '19 at 21:42
  • @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. – mahesh Jan 06 '20 at 03:48
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.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • 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); }); – JamesE Jun 16 '14 at 16:58
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.

Robin Daugherty
  • 7,115
  • 4
  • 45
  • 59
Pavlo Sadovyi
  • 79
  • 1
  • 5
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');
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