PM2 helps manage NodeJS:

sudo npm install pm2 -g

Then to start a Node.js process, do this and don't worry about it failing and exiting:

pm2 app.js

Instead of:

node app.js

PM2 has other benefits - ability to see all running processes, pause some, resume later, debug, run multiple threads, etc.

What Port?

Servers usually serve incoming visitor web traffic on port 80. So, is the same as

However, when starting a web server as a developer on your local Macbook, ports below 1000 are blocked. So, running a Node webserver on port 80 will not work. The browser url will try to go to port 80, but that request will not find its way to your system process. So, lets run our Node.js script on port 1080 instead.

We have to redirect all incoming requests from port 80 to port 1080. This is simple to do with these commands:

iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 1080
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 1443
ufw allow 80/tcp
ufw allow 443/tcp

Add these commands to run before your app starts. I put it in {codebase}/_cron/, which immediately after fixing these port numbers, pulls my latest codebase from GIT, then starts my node process with pm2 start api.js -i max

results matching ""

    No results matching ""