| Start |
|---|
| Setting NODE_ENV="production" Production deployments will vary in many ways, but a standard convention when deploying in production is to define an environment variable called Runtime flagsAny code running in your application (including external modules) can check the value of DependenciesWhen the For setting method 1: set NODE_ENV for all node apps Windows : Linux or other unix based system : This sets method 2: set NODE_ENV for current app This will set method 3: create This uses the idea explained here (opens new window). Refer this post for more detailed explanation. Basically you create To avoid writing a bash script, the env-cmd (opens new window) package can be used to load the environment variables defined in the method 4: Use This package (opens new window) allows environment variables to be set in one way for every platform. After installing it with npm, you can just add it to your deployment script in |
| Manage app with process manager It's a good practice to run NodeJS apps controlled by process managers. Process manager helps to keep application alive forever, restart on failure, reload without downtime and simplifies administrating. Most powerful of them (like PM2 (opens new window)) have a built-in load balancer. PM2 also enables you to manage application logging, monitoring, and clustering. PM2 process managerInstalling PM2:
Process can be started in cluster mode involving integrated load balancer to spread load between processes:
While having multiple users in production, its must to have a single point for PM2. Therefore pm2 command must be prefixed with a location (for PM2 config) else it will spawn a new pm2 process for every user with config in respective home directory. And it will be inconsistent. Usage:
|
| Deployment using process manager Process manager is generally used in production to deploy a nodejs app. The main functions of a process manager are restarting the server if it crashes, checking resource consumption, improving runtime performance, monitoring etc. Some of the popular process managers made by the node community are forever, pm2, etc. Forvever
Install Run application : This starts the server and gives an id for the process(starts from 0). Restart application : Here Stop application : Similar to restart, For more commands : https://www.npmjs.com/package/forever (opens new window) |
| Deployment using PM2
Install Then, run the Following commands are useful while working with List all running processes: Stop an app: Restart an app: To view detailed information about an app: To remove an app from PM2’s registry: |
| Using different Properties/Configuration for different environments like dev, qa, staging etc. Large scale applications often need different properties when running on different environments. we can achieve this by passing arguments to NodeJs application and using same argument in node process to load specific environment property file. Suppose we have two property files for different environment. Following code in application will export respective property file which we want to use. We give arguments to the application like following if we are using process manager like forever than it as simple as |
| Taking advantage of clusters A single instance of Node.js runs in a single thread. To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node.js processes to handle the load. |
Read Article
https://develop.horje.com/learn/1434/reference