4

I have a react + node app which I need to deploy. I am using nginx to serve my front end but I am not sure what to use to keep my nodejs server running in production.

The project is hosted on a windows VM. I cannot use pm2 due to license issues. I have no idea if running the server using nodemon in production is good or not. I have never deployed an app in production, hence I have no idea about appropriate methods.

Abdul Ahad
  • 1,221
  • 2
  • 16
  • 28

3 Answers3

3

You may consider forever or supervisor.

Check this blog post on the same.

rajesh-nitc
  • 5,049
  • 3
  • 25
  • 33
  • So just to make sure, using nginx for front end and forever for back-end, is it good practice and maintainable? I mean if I need to make changes to my front and back end, is it hassle-free? – Abdul Ahad Jun 28 '19 at 08:45
  • 1
    @AbdulAhad serving your react app through nginx is a best practice and forever for nodejs is a good practice too. yes, try it - should be hassle-free – rajesh-nitc Jun 28 '19 at 08:46
  • Also, what is the issue with nodemon. I mean, why should I prefer forever to nodemon? – Abdul Ahad Jun 28 '19 at 08:50
  • 1
    @AbdulAhad nodemon can be used too but it is mostly for scenarios when you make a code change and expect server to restart automatically. Ideal for development work. For production, people seem to prefer pm2, forever, supervisor – rajesh-nitc Jun 28 '19 at 08:58
  • Got it. If you could give a little more info, I still do not see the issue with nodemon, additionally I see it has more support and fewer issues than forever. Pardon the questioning, I really want to make sure I am doing the right thing – Abdul Ahad Jun 28 '19 at 09:01
  • @AbdulAhad see this blog post https://ifelse.io/2015/09/02/running-node.js-apps-in-production/ and also https://stackoverflow.com/questions/16369018/forever-nodemon-running-together – rajesh-nitc Jun 28 '19 at 09:07
  • 1
    I did see this, hence the question why not nodemon. – Abdul Ahad Jun 28 '19 at 09:10
  • 1
    Okay. Also this helped me [link](https://eladnava.com/deploying-resilient-node-js-apps-with-forever-and-nginx/) – Abdul Ahad Jun 28 '19 at 09:13
2

You can also use docker. You can create multiple docker containers that will run your node server. Now at the nginx level at your host machine you can do load balancing configuration which will route the traffic equally to different docker node containers this will improve your availability and scalability, In heavy traffic you just need to increase the number of docker node containers as and when required. I guess initially 2 containers will be enough to handle traffic (depends on your use case though).

Note:- You can also use forever or supervisor as suggested by @Rajesh Gupta inside your docker containers for running node server. We use PM2 for that.

If you have a database then you can create a separate docker container for the database and map it to a volume in your host machine.

You can learn about docker from here.

Also you can read about load balancing in nginx from here.

Further more to improve your availability you can add a caching layer in between nginx and docker containers. Varnish is the best caching service i have used till date.

PS:- We use a similar but more advanced architecture to run our Ecommerce application that generates 5-10k orders daily. So this is a tested approach with 0 downtime.

Sagar Khan
  • 302
  • 3
  • 13
1

Try to dockerize the whole app including the db, caching server (if any) etc. Here are some examples why:

  • You can launch a fully capable development environment on any computer supporting Docker; you don't have to install libraries, dependencies, download packages, mess with config files etc.

  • The working environment of the application remains consistent across the whole workflow. This means the app runs exactly the same for
    developer, tester, and client, be it on development, staging or
    production server. In short, Docker is the counter-measure for the
    age-old response in the software development: "Strange, it works for me!"

Every application requires a specific working environment: pre-installed applications, dependencies, data bases, everything in specific version. Docker containers allow you to create such environments. Contrary to VM, however, the container doesn't hold the whole operating system—just applications, dependencies, and configuration. This makes Docker containers much lighter and faster than regular VM's.

Rubin bhandari
  • 1,873
  • 15
  • 20