41

I would like to do something like:

app.On_All_Incoming_Request(function(req, res){
    console.log('request received from a client.');
});

The current app.all() requires a path, and if I give for example this / then it only works when I'm on the homepage, so it's not really all..

In plain node.js it is as simple as writing anything after we create the http server, and before we do the page routing.

So how do I do this with express, and what is the best way to do it?

Chenmunka
  • 685
  • 4
  • 21
  • 25
Adam Halasz
  • 57,421
  • 66
  • 149
  • 213
  • here's a tutorial I wrote on [writing express middleware](https://mguida.com/blog/writing-express-middleware/) to achieve this – mguida Jan 28 '20 at 19:34

4 Answers4

58

Express is based on the Connect middleware.

The routing capabilities of Express are provided by the router of your app and you are free to add your own middlewares to your application.

var app = express.createServer();

// Your own super cool function
var logger = function(req, res, next) {
    console.log("GOT REQUEST !");
    next(); // Passing the request to the next handler in the stack.
}

app.configure(function(){
    app.use(logger); // Here you add your logger to the stack.
    app.use(app.router); // The Express routes handler.
});

app.get('/', function(req, res){
    res.send('Hello World');
});

app.listen(3000);

It's that simple.

(PS : If you just want some logging you might consider using the logger provided by Connect)

Rahman Kalfane
  • 1,717
  • 17
  • 17
  • hey, that was very easy. I'm using express with node.js for a while, but I never thought about this :-) thanks bro! – Adam Halasz Aug 31 '11 at 21:44
  • 1
    but @Raynos doesn't that has to go trough the routing process, and regular expressions etc...? – Adam Halasz Aug 31 '11 at 21:47
  • @CIRK it adds middleware to `app.router` instead of the server. I doubt it's significantly less efficient – Raynos Aug 31 '11 at 21:51
  • 6
    @Raynos I feel like you should add an answer with the `app.all("*", cb)` solution, including discussion of why/why not to use it (I assume mostly stylistic). It is relevant to know the options. – Niels Abildgaard Jan 12 '15 at 12:05
  • `(PS : If you just want some logging you might consider using the logger provided by Connect)` `connect.logger` is now [morgan](https://github.com/expressjs/morgan) Use something like `app.use(require('morgan')('combined')); – CrazyPyro Apr 09 '15 at 08:04
  • 3
    `app.configure` is not a function? – theonlygusti Nov 11 '17 at 15:08
10

You should do this:

app.all("*", (req, res, next) => {
    console.log(req); // do anything you want here
    next();
});
Mike
  • 14,010
  • 29
  • 101
  • 161
Abadis
  • 2,671
  • 5
  • 28
  • 42
4

You can achieve it by introducing a middleware function. app.use(your_function) can be of help. app.use with accept a function that will get executed on every request logged to your server. Example:

app.use((req, res, next) => {
    console.log("req received from client");
    next(); // this will invoke next middleware function
});
Mike
  • 14,010
  • 29
  • 101
  • 161
Tarun Rawat
  • 244
  • 3
  • 9
2

Express supports wildcards in route paths. So app.all('*', function(req, res) {}) is one way to go.

But that's just for route handlers. The difference is that an Express route handler is expected to send a response, and, if it doesn't, Express will never send a response. If you want to do something without explicitly sending a response, like check for a header, you should use Express middleware. app.use(function(req, res, next) { doStuff(); next(); }

vkarpov15
  • 3,614
  • 24
  • 21