You may often want different settings for development environment and a different settings in the production environment. Also you could keep pushing the files to server for testing. So you’ll need to test your application in two different environments: development & production
In situations like this, you could have environment specific configurations using express. In this video tutorial, I’ll be walking you through setting up configurations for these environments using Express, web framework of Node.js.
Main application file
app.js
var express = require('express');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view cache', true);
// app.enable('view cache');
Here we create an express object, using which we set the configurations.
First we set the port number to whatever is set by the server – which is present in the environment variable process.env.PORT if it is not set use 3000 as port number.
Next, assign views a directory: root directory( __dirname ) and views folder. You could create any folder inside root and assign it to views. Then, set default view engine as Jade. Next we enable the view cache – btw View caching is useful in production environment.
Development Environment
app.js
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
Here we could list all the settings needed for our development environment.
Production Environment
app.js
// production only
if ('production' == app.get('env')) {
app.enable('view cache');
}
Here we could list all the settings needed for our production environment.
Configuring Express Application: Node.js
Note: To run with specific environment settings configuration use the following command while running your Node.js application
C:/>cd node
C:/node>NODE_ENV = production node app.js