Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Express.js Middleware


Express.js Middleware are different types of functions that are invoked by the Express.js routing layer before the final request handler. As the name specified, Middleware appears in the middle between an initial request and final intended route. In stack, middleware functions are always invoked in the order in which they are added.

Middleware is commonly used to perform tasks like body parsing for URL-encoded or JSON requests, cookie parsing for basic cookie handling, or even building JavaScript modules on the fly.


What is a Middleware function

Middleware functions are the functions that access to the request and response object (req, res) in request-response cycle.

A middleware function can perform the following tasks:

  • It can execute any code.
  • It can make changes to the request and the response objects.
  • It can end the request-response cycle.
  • It can call the next middleware function in the stack.

Express.js Middleware

Following is a list of possibly used middleware in Express.js app:

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

Let’s take an example to understand what middleware is and how it works.

Let’s take the most basic Express.js app:

File: simple_express.js

  1. var express = require(‘express’);
  2. var app = express();
  3. app.get(‘/’, function(req, res) {
  4.   res.send(‘Welcome to Kreationnext!’);
  5. });
  6. app.get(‘/help’, function(req, res) {
  7.   res.send(‘How can I help You?’);
  8. });
  9. var server = app.listen(8000, function () {
  10.   var host = server.address().address
  11.   var port = server.address().port
  12. console.log(“Example app listening at http://%s:%s”, host, port)
  13. })

You see that server is listening.

Now, you can see the result generated by server on the local host http://127.0.0.1:8000

Output:

expressjsmiddleware2

Let’s see the next page: http://127.0.0.1:8000/help

Output:

expressjsmiddleware3

Let’s see the next page: http://127.0.0.1:8000/help

Output:

expressjsmiddleware4

Note: You see that the command prompt is not changed. Means, it is not showing any record of the GET request although a GET request is processed in the http://127.0.0.1:8000/help page.

Use of Express.js Middleware

If you want to record every time you a get a request then you can use a middleware.

See this example:

File: simple_middleware.js

  1. var express = require(‘express’);
  2. var app = express();
  3. app.use(function(req, res, next) {
  4.   console.log(‘%s %s’, req.method, req.url);
  5.   next();
  6. });
  7. app.get(‘/’, function(req, res, next) {
  8.   res.send(‘Welcome to Kreationnext!’);
  9. });
  10. app.get(‘/help’, function(req, res, next) {
  11.   res.send(‘How can I help you?’);
  12. });
  13. var server = app.listen(8000, function () {
  14.   var host = server.address().address
  15.   var port = server.address().port
  16.   console.log(“Example app listening at http://%s:%s”, host, port)
  17. })

You see that server is listening.

Now, you can see the result generated by server on the local host http://127.0.0.1:8000

Output:

expressjsmiddleware6

You can see that output is same but command prompt is displaying a GET result.

Go to http://127.0.0.1:8000/help

expressjsmiddleware8

As many times as you reload the page, the command prompt will be updated.

expressjsmiddleware10

Note: In the above example next() middleware is used.

Middleware example explanation

  • In the above middleware example a new function is used to invoke with every request via app.use().
  • Middleware is a function, just like route handlers and invoked also in the similar manner.
  • You can add more middlewares above or below using the same API.

Comments

comments