Stack Exchange
Express middleware not being called
I'm using express router to build the routes in my application and I'm trying to apply custom middleware to each route so the middleware will only be called when urls are hit for that specific router. Here's my code:
const express = require('express');
const router = express.Router();
const authentication = require('./helpers/authentication');

const registerController = (app, path, router, middleware) => {
  middleware.forEach((routerMiddleware) => {
    router.use(routerMiddleware);
  });
  app.use(path, router);
};

const testMiddleware = (request, response, next) => {
  console.log(request.originalUrl);
  next();
};

module.exports = {
  init: (app) => {
    registerController(app, '/auth', require('./authenticationController'), [testMiddleware]);
    registerController(app, '/customer', require('./customerController'), [authentication, testMiddleware]);
    registerController(app, '/filter', require('./filterController'), [authentication, testMiddleware]);
    registerController(app, '/', require('./homeController'), [testMiddleware]);
    registerController(app, '/inventory', require('./inventoryController'), [authentication, testMiddleware]);
    registerController(app, '/settings', require('./settingsController'), [authentication, testMiddleware]);
  }
};

I've built the registerController method so I can assign middleware in different configurations to the different routers. Each require statement that pulls in a file name xxxController is returning a router like this:
const router = require('express').Router();
const fs = require('fs');

router.get('/', (request, response) => {
  const file = fs.readFileSync(__dirname + '/../index.html', "utf8");
  response.send(file);
});

module.exports = router;

I added the test middleware to see if the simplest middleware would work, but it's not being called.  What am I doing wrong here?

Actually, the different calls aren't using the same instance of router.  You probably think that because there is a reference to express router at the top of the file.  This was a mistake on my part as that is now an unused variable. Each call to registerController takes a router as it's third param and in each call I'm passing in the return value of a require statement.  All of these require statements return a different instance of express router.

When you are constructing an Express Router, the order in which you set up the routes and add middleware is very important.  For instance, if you add a route, then add middleware, the add more routes, only the routes that were added after the middleware was added will cause the middleware to be called. The first route that was setup prior to the middleware being setup will not trigger the middleware to be called.
So the problem with my code above is that I was building out all the routes then adding in the middleware, assuming that all middleware added to the router would be called for all routes.
The solution was to refactor and ensure I was adding the middleware at the beginning of each router.

The reason you're not seeing the routerMiddleware being used is because you have a router.get() in the controller file you've shown that is already responding to the request. Now if you had a POST request instead, then your routerMiddleware will be called because there is no router.post() in your controller file.
Express evaluates routes in the order in which they were added to the router/app.
