Middleware :Backbone of Express.js Part 1

 

Now, you can deploy your Node.js app to App Engine standard environment

 

Today we will discuss most important topic for developing web application in Node.js using Express framework.Express is a minimal and flexible framework for web and mobile application.A Express application is essentially a series of Middleware function calls one after another.

Middleware is core concept behind Express.js.

Because Middleware is core concept behind experss.js in case request processing and routing so,with better understanding of Middleware can be able help us to construct maintainable application with less line of code.
Middleware in Express
When the Express.js is running,it is listening to request.Each request is processed according to a defined chain of Middleware and routes, starting from top to bottom.

Request,and Response objects can be accessed by Middleware.

Create Middleware

For creating middleware we have require three parameters first parameter is req i.e requesting object second parameter is res i.e. responding object and our third parameter is next is a function and used for continuing for next Middleware.We will explain i detail latter in upcoming examples.

Example :

Containing Single middleware

In case of single Middleware our application contains single Middleware as we know that Middleware contains three parameters as we discussed previously first parameter is req i.e requesting object second parameter is res i.e. responding object and our third parameter is next is a function and used for continuing for next Middleware because in this case our application contains only one Middleware so our third parameter is optional.


//Declaring express module
var express = require('express');
var app = express();
//Create Middleware
app.use('/',function(req,res){
 console.log("hello World");
})
app.listen(3000);

Run application

Open command prompt and run command


node middleWare.js

After that to see your result open your browser and copy link.


localhost:3000

After refreshing browser you got hello world message on your console.

Containing Multiple Middlewares

When our application contains multiple Middleware then in that case our flow will be from top to bottom and for performing such operation by our Middleware contains three parameters as in above example contain two parameters and in this case our third parameter is next.

Note: next is absolutely a function which is used to continue flow inside our application from top to bottom from one middleware to another.

middleWare.js


var express = require('express');
var app = express();
//Creating first middleware having 3 parameters
app.use('/',function(req,res,next){
 console.log("Start");
 next();
})
//Creating second middleware having 3 parameters
app.get('/',function(req,res,next){
 res.send('Hello middleware');
 next();
})
/*Creating third middleware having 2 parameters it is not mendatory that our
last middleare doesnot contain only two parameters it can also conation third paremeter
and it is good for your better paractice. 
*/
app.use('/',function(req,res){
 console.log("End");
})
//it is port number
app.listen(3000);

Open command prompt and run command


node middleWare.js

After that to see your result open your browser and copy link


localhost:3000

And you will obtain result as shown bellow
Middleware in Express

Functionality of Middlware

  1. It can be able to execute any code.
  2. It make changes to request and response process.
  3. It stops after ending request response cycle.
  4. It can call next Middleware function in the stack.

On the basis of Application in Express.js Middleware can be categorized in following types:

  • Application-level middleware
  • Route level middleware
  • Error handling middleware
  • Built-in middleware
  • Third-party middleware

Leave a Reply

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