Home > Software engineering >  app.use() does not log any incoming requests
app.use() does not log any incoming requests

Time:02-28

I have a few api calls set up via express and router. I am trying to debug and understand the working of the router. I have a logger set up, and trying to log all the api calls. However, the calls are never logged. All the api endpoints get called and the application works as expected though. Below is an example of what I am trying to do. Can someone please let me know what could be missing here?

const logger = require('./config');
const app = express();

// routes needed for the app
app.use(require('./routes/apis'));

app.use('/api', (req, res, next) => {
   logger.info('in /api call');       
});

CodePudding user response:

you need to change the order of the middleware:

const logger = require('./config');
const app = express();
app.use('/api', (req, res, next) => {
   logger.info('in /api call');       
});
// routes needed for the app
app.use(require('./routes/apis'));
  • Related