Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PM2 (Node.js) not listening on specified port

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.

like image 218
JamesE Avatar asked Jun 12 '14 19:06

JamesE


People also ask

What port does PM2 run on?

Starting from PM2 3.2, we changed the networking connection by using a direct Websocket connection to our server on the port 443, so you only need OUTBOUND on port 443 TCP open.

How do I run node js on different ports?

Create NodeJS Server js file. Observe the last line where we specify listen(80). This will ensure that your NodeJS server runs on port 80. If you want to run NodeJS on different port, change 80 to your required port number (e.g 8080).


3 Answers

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.

like image 65
JamesE Avatar answered Oct 06 '22 19:10

JamesE


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.

like image 3
Peter Lyons Avatar answered Oct 06 '22 18:10

Peter Lyons


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.

like image 1
Pavlo Sadovyi Avatar answered Oct 06 '22 19:10

Pavlo Sadovyi