Search code examples
node.jsexpressnode.js-connect

How to write an express route middleware that will only catch actions?


I'm using the following middleware :

  app.use(function(req,res,next){
     console.log(req.url)
     next()
  });

but it catches every request, including images/css/js etc...

If I just want to catch the actions, what should I do?


Solution

  • You could put your "static handler" middleware before this one, if the request matches the static middleware route, it should not reach your middleware at all.

    So try:

    app.use(express.static(__dirname + '/public'));
    app.use(function(req,res,next){
      console.log(req.url)
      next()
    });