Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to set a Node/Express app to live or production mode

Tags:

I'm currently in the process of making my node/express app into a production deployment, and as part of this, I need to make it run in a production friendly mode (e.g. fewer debugs to stdOut, write logs to different places, tell users less when errors occur etc.).

I'm struggling a bit with this, as whenever I set a variable of virtually any kind to invoke a 'production' mode, it doesn't take affect for the program as it runs.

When launched in dev mode, my code runs through Gulp, and runs this script:

#!/usr/bin/env node var debug = require('debug')('ascema'); var app = require('../app');  app.set('port', process.env.PORT || 3000);  var server = app.listen(app.get('port'), function() {     debug('Express server listening on port ' + server.address().port); }); 

Which, as you know, is just the generated launch script from the express generator.

In order to run it in live mode, I created an alternative startup for the server to use (I could hardly use gulp anyway) and live.js runs this:

#!/usr/bin/env node var app = require('./app.js');  app.set('env', 'production');  console.log('running on Port 3000');  var server = app.listen(3000); 

But, when I use app.get('env') anywhere in the app (e.g. in app.js or in it's various dependencies) it still returns 'development' and so none of my production tasks happen.

What am I doing wrong here?

Many thanks.

like image 556
Owen C. Jones Avatar asked May 01 '15 09:05

Owen C. Jones


People also ask

How do I run a Node app in production mode?

You can signal Node. js that you are running in production by setting the NODE_ENV=production environment variable. in the shell, but it's better to put it in your shell configuration file (e.g. . bash_profile with the Bash shell) because otherwise the setting does not persist in case of a system restart.


2 Answers

You have to set the NODE_ENV variable on the command line when you execute your NodeJS application.

For example:

NODE_ENV=production node app.js 

Also, NODE_ENV is an environment variable so if set it in the environment on your server, you will not need to provide it every time you execute your application so node app.js will do.

You can set your environment variables in the /etc/environment file. Here are more details on that.

like image 91
Noman Ur Rehman Avatar answered Sep 16 '22 15:09

Noman Ur Rehman


By using NPM you might to be used the follow scripts in the package.json:

  "scripts": {     "start": "nodemon ./bin/www",     "dev_win": "set NODE_ENV=development && node ./bin/www >> /romba/log/api.log 2>> /romba/log/error.log",     "prod_win": "set NODE_ENV=production && node ./bin/www >> /romba/log/api.log 2>> /romba/log/error.log"     "prod_nix": "NODE_ENV=production node ./bin/www >> /romba/log/api.log 2>> /romba/log/_error.log"   },... 

To start one of the script use the command:

 npm run-script prod_win 

In the JavaScript code I check the condition:

process.env.NODE_ENV.indexOf('production') > -1 

or with Express.js

app.use((err, req, res, next) => {   req.app.get('env') === 'development' ? ... 
like image 43
Pax Beach Avatar answered Sep 18 '22 15:09

Pax Beach