Below is our molded ourpage.js from previous post :
var express = require('express') // import express module
var ourPage = express.Router() // import Router object
ourPage.use(function(req, res, next) { // use 'use' function to declare a middleware and pass next as extra argument
console.log("First MiddleWare")
next() // must call next() method. To let know the express that there is extra work to be done
})
ourPage.get('/', function(req, res, next) { // handle the route
res.send("Hi");
console.log("Second MiddleWare");
next();
})
ourPage.use(function(req, res) { // no need to include next argument if you don't want further processing
console.log("Third MiddleWare")
})
module.exports = ourPage
Console Will Have following Output:-
First MiddleWare
Second MiddleWare
Third MiddleWare
Above situation can be understood by following diagram:
