33

What is the easiest method to let express know what environment I am in? E.g. I want do do the below to make a connection to redis depending on what env I am in. Can this be done from the command line?

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  var r = require("redis").createClient(6379,'127.0.0.1');
});
app.configure('production', function(){
  app.use(express.errorHandler());
  r = redis.createClient(6379,'46.137.195.230', { detect_buffers: true });
});
Grace Huang
  • 5,355
  • 5
  • 30
  • 52
Tampa
  • 75,446
  • 119
  • 278
  • 425

5 Answers5

30

Your approach is ok, but you can make something more generic, like storing the config data for Redis in a file or passing the host and port like arguments:

node app.js REDIS_HOST REDIS_PORT

Then in your app you can grab them using process.argv:

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  var r = require("redis").createClient(process.argv[2], process.argv[3]);
});
app.configure('production', function(){
  app.use(express.errorHandler());
  var r = require("redis").createClient(process.argv[2], process.argv[3], { detect_buffers: true });
});

Update:

Express will know in what environment you're in by looking at the NODE_ENV variable (process.env.NODE_ENV): https://github.com/visionmedia/express/blob/master/lib/application.js#L55

You can set that variable when starting the app like so: NODE_ENV=production node app.js (recommended), setting process.env.NODE_ENV manually in your node app before the Express code or putting that env var in ~/.profile like Ricardo said.

alessioalex
  • 62,577
  • 16
  • 155
  • 122
  • 1
    You could go further by defining the defaults for those parameters in your package.json file or another configuration file. – Julian Knight May 23 '12 at 14:45
25

To expand on the idea of using a config.json file:

// config.json
{
  "development": {
    "redisPort": 6379,
    "redisHost": "127.0.0.1",
    "errorHandlerOptions": {"dumpExceptions": true, "showStack": true}
  },
  "production": {
    "redisPort": 6379,
    "redisHost": "46.137.195.230",
    "errorHandlerOptions": {"dumpExceptions": false, "showStack": false}
  }
}

Load the config file and switch based on env.

// app.js
var config = require('./config.json')[app.get('env')];
app.use(express.errorHandler(config.errorHandlerOptions));
var r = require("redis").createClient(config.redisPort,config.redisHost);

Make sure the NODE_ENV is set on each server (see other answers, one way: NODE_ENV=production node app.js), and this way the config variable has the settings appropriate to the server it runs on.

Sean
  • 15,561
  • 4
  • 37
  • 37
19

Just set the NODE_ENV environment variable to production or development, as seen in express' docs: http://expressjs.com/guide.html#configuration

I just leave NODE_ENV=development in the dev machine's ~/.profile (.bashrc or bash_profile on linux), and do the same for production ones.

Ricardo Tomasi
  • 34,573
  • 2
  • 55
  • 66
  • 6
    Just a heads up. The link you posted is now broken. I belive the eqivelant is http://expressjs.com/api.html#app.set – ChristopherStrydom Nov 22 '15 at 03:54
  • 1
    Or possibly: http://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production – tgf Feb 22 '18 at 01:24
9

I did somthing even more comprehensive by ordering the sources of such parameters :

    var env = process.argv[2] || process.env.NODE_ENV || 'development'
    var mongourl = process.argv[3] || process.env.NODE_DB || 'mongodb://localhost/default'
    var port = process.env.PORT || 9001

This way you can use command line args, env settings and default values.

Arnaud Rinquin
  • 4,296
  • 1
  • 30
  • 51
  • 1
    With your technique above, unless you manually do `process.env.NODE_ENV = process.argv[2]` when passing an env param on the command line, Express will still think your in process.env.NODE_ENV mode (dev by default). – alessioalex May 23 '12 at 08:05
  • True. I may have to change that. Thank you for your observation. – Arnaud Rinquin May 23 '12 at 09:16
  • 1
    Replacing `var env =` with `var env = process.env.NODE_ENV =` worked for me. – Ben Hutchison Apr 18 '14 at 03:11
0

Use config module that allows to create multiple config files https://www.npmjs.com/package/config

Eg default.json , production.json

And start your server wIth

export set NODE_ENV=production && npm start

Ashutosh Jha
  • 15,451
  • 11
  • 52
  • 85