The Expressjs middleware is very similar to a Request handler. The sequence of processing steps a request passes at the server is been managed by middleware function.
Rather than having one big request handler to manage requests. Here we can actually have several small request handlers that can deal with a small set of code. So, it can deal with logging, authorizing, and executing those codes in sequential order.
What is Expressjs Middleware?
- Middleware is the middle layer between the software and the server.
- It executes the requests during the lifecycle of a request to the Expressjs server.
- Each middleware has access to the HTTP request and response for each route it’s attached to.
- An Expressjs middleware will terminate the HTTP request or pass it on to another middleware function using the next function.
-
Chaining of middleware is something that allows you to segment your code and create reusable Expressjs middleware.
Read: 5 ways to make HTTP requests in Nodejs
NODEJS: How Expressjs middleware works?
Expressjs Middleware functions can read the request object (req), the response object (res), and the next function in the request-response cycle of an application.
We can call each middleware as function(req,res,next). The basic rules that middleware can do are:
- Read the request (req), modify req and res
- Respond by calling res.send()
- Passing processed requests to the next middleware in line (calling next() ).
The Middleware function can perform the following task, they are:
- Execute any code.
- It has access to change requests and response objects.
- End the request-response cycle.
- Call the next middleware in the stack.
Let’s see what is middleware and how it works below with example:
Let’s take the most basic Express.js app:
File: simple_express.js
var express = require('express'); var app = express(); app.get('/', function(req, res) { res.send('Welcome to Webnexs!'); }); app.get('/help', function(req, res) { res.send('How can I help You?'); }); var server = app.listen(8000, function () { var host = server.address().address var port = server.address().port console.log("Example app listening at http://%s:%s", host, port) })
Node.js command prompt - node simple_middleware.js
c:\Users\webnexs\Desktop>node simple_middleware.js Example app listening at http://: : :8000
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:
http://127.0.0.1:8000/help Welcome to Webnexs
Let’s see the next page: http://127.0.0.1:8000/help
Output:
http://127.0.0.1:8000/help How can I help you??
Node.js command prompt - node simple_middleware.js
c:\Users\webnexs\Desktop>node simple_middleware.js Example app listening at http://: : :8000
Note: Now you can see that the output in the command prompt is not changed. It shows that it’s not showing any record of GET request processed in the http://127.0.0.1:8000/help page.
Use of Expressjs Middleware
You can use a middleware to record every time for every request.
File: simple_middleware.js
var express = require('express'); var app = express(); app.use(function(req, res, next) { console.log('%s %s', req.method, req.url); next(); }); app.get('/', function(req, res, next) { res.send('Welcome to Webnexs!'); }); app.get('/help', function(req, res, next) { res.send('How can I help you?'); }); var server = app.listen(8000, function () { var host = server.address().address var port = server.address().port console.log("Example app listening at http://%s:%s", host, port) })
Node.js command prompt - node simple_middleware.js
c:\Users\webnexs\Desktop>node simple_middleware.js Example app listening at http://: : :8000
Below we show you the output by a server on the local host http://127.0.0.1:8000
Output:
http://127.0.0.1:8000
Welcome to Webnexs
Even if you get the same output, the command prompt is still displaying a GET result.
Node.js command prompt - node simple_middleware.js
c:\Users\webnexs\Desktop>node simple_middleware.js Example app listening at http://: : :8000 GET \ Go to http://127.0.0.1:8000/help
http://127.0.0.1:8000/help
How can I help you??
Node.js command prompt - node simple_middleware.js
c:\Users\webnexs\Desktop>node simple_middleware.js Example app listening at http://: : :8000 GET \ GET \help
The command prompt will update, whenever you reload the page, as shown below:
c:\Users\webnexs\Desktop>node simple_middleware.js Example app listening at http://: : :8000 GET \ GET \help GET \help
Note: next() middleware is used in the above example.
EXPRESSJS Middleware example explanation
- Using app.use(), a new function is used to call on every request in the above middleware example.
- Like route handler, Expressjs Middleware is a function that invoked as same as route handler.
Leave a Reply