The best way to install a Node / Express application in real time or production

I am currently running my node / express application in production deployment, and as part of this, I need to make it work in a mode convenient for production (for example, fewer debugs for stdOut, write logs in different places, tell users less when errors occur etc.).

I’m struggling with this a little, because whenever I set a variable of almost any type to call the “production” mode, it does not affect the program’s work when it starts.

When running in dev mode, my code goes 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 script run from the express generator.

To launch it in real time, I created an alternative launch for the server (I could hardly use gulp), 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 application (for example, in app.js or various dependencies in it), it still returns “development”, and therefore none of my production tasks arise.

What am I doing wrong here?

Many thanks.

+8
express
source share
1 answer

You must set the NODE_ENV variable on the command line when you run your NodeJS application.

For example:

 NODE_ENV=production node app.js 

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

You can set environment variables in the file `/ etc / environment '. More on this: https://help.ubuntu.com/community/EnvironmentVariables

+16
source share

All Articles