Middleware
Node.js Express also provides a special routing method, all()
, which is not mapped to any HTTP method. But it is used to load Middleware at a path, irrespective of the HTTP method being requested. For example, making HTTP GET
and POST
requests at http://localhost/middlewareexample
will execute the same all()
method shown in the following code:
expressApp.all('/middlewareexample', function (req, res) { console.log('Accessing the secret1 section ...'); });
Just like in .NET, we have OWIN middleware that can be chained in the request pipeline. In the same way, Node.js Express middleware can also be chained and can be invoked by calling the next middleware with a little change in the function signature. Here is the modified version that takes the next
parameter after the response object which provides a handler to the next middleware in the pipeline, defined in a sequence for a particular request path:
expressApp.all('/middlewareexample', function (req, res, next) { console.log...