Express has two types of middleware. The first is called before routes are run and the second is called between a route and its callback function.
Writing Basic Middleware
Writing middleware for either of these is simple and only involves the writing of a single function which can be passed to express. A basic middleware function for express looks like the following:
function testMiddleWare (request, response, next){
console.log(request);
console.log(response);
}
The above code doesn’t do much other than logging the current request and response data structures to the command line but it does give us the basic middleware pattern. Structure wise an express middleware function needs to have the following formal parameters:
- The request - which sometimes is named req
- The response - which is sometimes called res
- The next function which calls the next operation. Depending on where the middleware is added the next() function will either call the function attached to the requested route or the trigger the routs once the middleware has finished. This is why the next function is called at the very end of the function.
In order to globally include middleware before routs, you can add the middleware using expresses use() function like this:
app.use(testMiddleWare );
To add middleware to a route you add it to the app.get() or app.post() routing function using:
app.get('/test/get', testMiddleWare , function (req, res) {
res.send('ok');
});
or
app.post('/test/post', testMiddleWare , function (req, res) {
res.send('ok');
});
Notice how the testMiddleWare functions has between passed into the post/get URI string and the callback function without any formal parameters. This is because we are passing the testMiddleWare function by reference so that expresses internals can call the function.
Moving Middleware to a Separate File
No one wants to clutter up the server or route files with random middleware functions and so it is necessary to move middleware to an external file.
middleware.js
function testMiddleWare (request, response, next) {
console.log(request);
console.log(response);
next();
}
module.exports = testMiddleWare;
in your routing files or express set up file, you can then include it by
var testMiddleWare = require('./middleware.js');
Bundling Multiple Middleware Functions Into a Single File
It is also possible to import multiple middleware functions out of the same file. Exporting multiple middle ware functions is posible through using JSON based classes like:
middleware.js
module.exports = function (){
return {
testMiddleWare: function(request, response, next) {
console.log(request);
console.log(response);
next();
},
testMiddleWareTwo: function(request, response, next) {
console.log(request);
console.log(response);
next();
},
}
}
in your routing files or express set up file, you can then include it by
var MiddleWare = require('./middleware.js')();
app.use(MiddleWare.testMiddleWare );
app.use(MiddleWare.testMiddleWareTwo );
Zach Radloff lives on the Gold Coast and works as a software engineer.