I have written a code in NodeJS where when i hit the url, i need the server to pass through three middleware functions authentication, cookies, logging. Here it happens , but the console gets printed twice. Can you help me figure out why.
var express = require('express'); var app = express(); var router = require('express').Router(); /* Add the middleware to express app */ app.use (function authentication(req,res,next){ if(req.method === 'GET'){ console.log("Inside Authentication.js") next(); // If your don't use next(), the mmand won't go to the next function. } else{ console.log("Inside else Authentication.js") } }) app.use( function cookies(req,res,next){ if (req.method === 'GET'){ console.log("Inside cookies.js") next(); } else { console.log("Inside else cookies.js") } }) app.use( function logging(req,res,next){ if(req.method === 'GET'){ console.log("Inside Logging.js"); next(); } else{ console.log("Inside else of Logging.js"); } }) app.use(function(req,res) { res.send('Hello there !'); }); app.listen(8080); o/ p -
E:\NodeJSProject\middleware>node app.js Inside Authentication.js Inside cookies.js Inside Logging.js Inside Authentication.js Inside cookies.js Inside Logging.js