NodeJS server side application deployment cosiderations

199 views Asked by At

I am writing a nodejs application with Angular as my front end. For this I am having Git as my code management server. For client, I am doing minification and it is ready for production.

But I am not sure, how to prepare server side files for production. Do we need to just copy all the Git folders into production server?. Let me know the best way to deploy nodejs server application.

2

There are 2 answers

0
Qiaosen Huang On
  1. You could use pm2 as your daemon to keep your nodejs app up all the time.
  2. Try not to include node_modules in the repo, cause different machines have different setups/installations, you cannot tell if one package would work before you run it unless you npm install them.
  3. If you are familiar with Docker, use it, pre-bundle all (include node_modules) files into the docker image, and you do not need pm2 here, Docker itself can restart automatically. This is the ideal approach.
0
Strae On

It really depends on how you (or your company) want to organize the workflow and the size of the project.

Sometimes I too use a GIT repository, because then is really simple to update: just a git pull and (if server files got edits) a pm2 restart N command.

In this way, you dont have to install the whole development stack in order to compile (and minify) the bundles - I guess you work on your local machine where all the development tools are installed.

Keep in mind to use the --dev flag while installing packages that are only required in development mode, so you can keep the production server as slim as possible.

A good practice I found is to add some random tokens inside the final bundle filename (both for js and css) that get then injected inside the final html static files, to avoid the refresh the page loop.

Once you have the bundle files on your dev machine, just upload them to the server (ftp, git, rsync, sshfs mount, whatever you like) and (if server files got edits) restart/reload the node process (Im using pm2 for this, its really great). If you only edited client files, no reload is needed.

Starting from here, there a lot of ways more or less sophisticated to do the job, like git pipelines for example.. but depends on the situation.

Edit: this is a good article about task runner (gulp vs grunt vs vanilla npm), while may be a little off topic, it analyze some aspect of the common deployment process