Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js, express, and using development versus production in app.configure

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 }); }); 
like image 279
Tampa Avatar asked May 23 '12 06:05

Tampa


People also ask

What is the difference between app use and app get in ExpressJS?

app. get is called when the HTTP method is set to GET , whereas app. use is called regardless of the HTTP method, and therefore defines a layer which is on top of all the other RESTful types which the express packages gives you access to.

Can Node.js be used in production?

Node. JS is ideal for fast, lightweight, real-time web applications such as audio/video streaming, browser games, chats, collaboration tools social media, time trackers, and much more. For this reason, many companies decide to use Node. js in production.

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

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.

like image 126
alessioalex Avatar answered Sep 28 '22 04:09

alessioalex


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.

like image 23
Sean Avatar answered Sep 28 '22 02:09

Sean