Configuration of Express Application: Node.js

Advertisement:

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

development-production-environment-express-nodejs

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

1
2
3
4
5
6
7
8
9
10
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

1
2
3
4
// 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

1
2
3
4
// 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


You need to a flashplayer enabled browser to view this YouTube video

YouTube Link: http://www.youtube.com/watch?v=60xRnFSMRk8 [Watch the Video In Full Screen.]



Note: To run with specific environment settings configuration use the following command while running your Node.js application

1
2
C:/>cd node
C:/node>NODE_ENV = production node app.js
Advertisements

Leave a Reply

Your email address will not be published. Required fields are marked *