How to handle all requests in a filter context in Express?

Checking express documents I saw this kind of solution below:

app.all('/*', function(req, res) { console.log('Intercepting requests...'); }); 

It really intercepts the request and displays a message to the console. The problem is that the site’s execution process does not stop, the request does not end, and it seems to be in a loop. Is there another way to simulate filters on Express or can't do it right now?

Thanks!

+4
source share
1 answer

You should add next as a parameter to your function and then call it inside when you are done with logging

 app.all('/*', function(req, res, next) { console.log('Intercepting requests ...'); next(); // call next() here to move on to next middleware/router }) 
+8
source

All Articles