What are middlewares in express

17 / Feb / 2016 by Satyam Chaudhary 0 comments

Middleware is a simple javascript function with three parameters request, response and next

Apart from handling http request it can perform the following tasks:

  • Manipulate request and response objects
  • Perform isolated tasks
  • Terminate request flow by sending response
  • Pass the control to another middleware

We load middleware in express by use() of the express app.

var express = require('express');
var app = express();
app.use(function (req, res, next){
	// Your code
	// to pass the control invoke next() at the end
	next();	
});

Here,
– req is the request object.
– res is the response object.

Click Here to find github example

 

FOUND THIS USEFUL? SHARE IT

Leave a comment -