pm2, node, instances and ports

Viewed 6066

Sorry if this question is to 'newbie' but I can't see how to do it / how it works.

1/ I am creating an API using node.js, API serving some front apps

2/ To get it robust I use pm2 (great product), having nginx as proxy, fine

3/ But how could I handle multiple instance of my node.js api ? I see the 'cluter mode', fine, but what's happening while my node.js is listening on a single port (8080 for instance) ? If I configure 4 process / instances, they just can't all listen on the same port ah the same time ? ... so : do pm2 dispatch the calls between the instances (so acting as a kind of proxy indeed).

Any clue will be appreciated, how does it work ?

Greg

3 Answers

You can manage load using nginx also. here is the documentation for nginx load balance

after that you can run app with specific port using environment variable from .env file and access it like this way. ex: process.env.PORT

after that run app using pm2 this way

PORT=5003 pm2 start routes.js --name app5003
PORT=5002 pm2 start routes.js --name app5002
PORT=5004 pm2 start routes.js --name app5004
Related