Kevin
Kevin

Reputation: 25259

different ways to use express middleware

I can set express middleware like this:

app.use(function(req,res,next) {
    console.log("my middleware");
    return next();
});

And also like this:

app.get("/", function(req,res,next) {
     console.log("my other middleware");
},
function(req,res) {
     res.send("Test");
});

Aside from the fact that the first case is applied globally to all routes, and in the second case it is only applied to the specific route "/", do these behave identically?

Upvotes: 0

Views: 88

Answers (1)

Brad
Brad

Reputation: 163272

You have two differences. The first is correct... the first middleware is used for all requests and the second one is for anything matching your route of /.

The second difference is that the first middleware is used for all verbs, whereas the second middleware is only use for GET requests.

Upvotes: 3

Related Questions