Short introduction on Express.js 4 New Features

express

In this post we have tried to explain express.js 4 with some of it’s APIs.

First of all, the new features of Express.js V4 –

All the middlewares from connect are removed from Express. We have to bind all the middlewares in the project configuration file – package.json.

A typical package.json file will look like –


{
  "name": "starter-node-express",
  "main": "server.js",
  "dependencies": {
    "express": "~4.0.0",
    "morgan": "~1.0.0",
    "body-parser": "~1.0.0",
    "method-override": "~1.0.0",
    "mocha": "*",
    "should": ">= 0.0.1"
  }
} 

In the above file “body-parser” is declared as separate middlewere.

Other dependecies like

“morgan” is used for logging module
“express” is used for core express functionality
“mocha” is used for testing the project

and so on…

After declaring the package.json, we need to run – “npm install” to install the required modules for the project.

Now the settings of our server.js –


var express        = require('express'); // calling express module
var app            = express();          // The particular application is using express

var morgan         = require('morgan');  // for logging module 
var bodyParser     = require('body-parser'); // to pull request from a post
var methodOverride = require('method-override');

To use the static file location, we have to declare a global space location which is “public” folder here.



app.use(express.static(__dirname + '/public')); 	// set the static files location /public/img will be /img for users

app.use(morgan('dev')); 					// log every request to the console

app.use(bodyParser()); 						// pull information from html in POST

app.use(methodOverride()); 					// simulate DELETE and PUT

Below are different uses of get method, which are supported by Express.js still now.



//This is a simple get example
app.get('/api', function (req, res) {
   res.send('Express 4 API is running');
});

// Call the root
app.get('/', function(req, res){
  res.send('hello world');
});

//This is request parameter example
app.get('/params', function (req, res) {
   console.log(req.param('value'));
   var param = req.param('value');
   res.send('This is params example '+param);
});

The above example will work for “http://localhost:8080/params?value=first” and the result will be in the browser
as “This is params example first”



//This is request originalurl example
app.get('/paramsoriginal', function (req, res) {
   console.log(req.originalUrl);
   var param = req.originalUrl;
   res.send('This is params example '+param);
});

The above example will work for “http://localhost:8080/paramsoriginal?value=first” and the result will be in the browser as “This is params example /paramsoriginal?value=first”



//Check if the request was issued with the "X-Requested-With" header field set to "XMLHttpRequest" (jQuery etc). req.xhr
app.get('/paramsxhr', function (req, res) {
   console.log(req.xhr);
   var param = req.xhr;
   res.send('This is params example '+param);
});

For the above example, if “http://localhost:8080/paramsxhr?value=first” is called from browser address bar,
the result will be in the browser as “This is params example false”

Now the improved router in express.js V4. Instead of using app.get(), app.post() etc…

We can now call the app.route like –



app.route('/students')
  .get(function(req, res) 
   {
 	console.log('the get method for student');
        res.send('the get method for student');
   })
   .post(function(req, res) 
   {
        console.log('the post method for student');
        res.send('the post method for student'); 
   })

Here all the http methods will work for ‘students’ path in the project.

Another powerful inclusion of public api for express router can be found in below example.



// A variable for express router
var teacher = express.Router();

// get method applied to the router on default path
teacher.get('/', function(req, res) {
	res.send('im the teacher landing page!');	
});

// get method in a sub section/path of 'teacher' path 
teacher.get('/findus', function(req, res) {
	res.send('Here you will find teachers');	
});

// apply the routes to application
app.use('/teachers', teacher);

So in the above section the urls will be –

http://localhost:8080/teachers/

and

http://localhost:8080/teachers/findus

So here the router will act for a new separate module in the node application statrting with a distinct path.

All the funstionalities related to particular modules will be more organised here.

Here is the Github gist –

More updates will be available in this post as we learn more regarding those new features.

Have you used other features in express.js which are new? You can comment the same.

Leave a Reply

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