extremq / shobi-node

shobi website made using node.js, express and mongodb
MIT License
4 stars 0 forks source link

Enhancement - Consider using a process manager on production environment #3

Open SuspiciousLookingOwl opened 3 years ago

SuspiciousLookingOwl commented 3 years ago

As stated on [Express.js Performance best Practices], it's recommended to use a process manager like PM2 or Strongloop. Using a process manager allows you to run your Node app in cluster mode, making use of multi cores on the server, auto restart if it crashes, and more.

I personally recommend PM2.

To use PM2:

  1. Install the package globally npm i -g pm2, optionally you can also add it to devDep (npm i -D pm2)
  2. Modify your start script to "start": "pm2 start server.js", (or pm2 start server.js -i max to run on cluster mode and create node processes as many as the available CPUs, or pm2 start server.js -i 2 to run 2 processes)

EDIT: Since you are using stateful session (express-session) running the process on cluster mode won't work (it will work, but you will run into issue where user lost their session and have to login again), you can read more about it here

extremq commented 3 years ago

Thank you for the suggestion! I will definitely implement this in a pull request. This is my first node.js app ever and I'm learning a TON of things, so I'm grateful you had a look over it, haha! I will have to look over the session issue too. I'll make sure to use the cores. :)