Max Koretskyi
Max Koretskyi

Reputation: 105547

How to not trigger router middleware

Express states here that:

A route will match any path that follows its path immediately with a “/”. For example: app.use('/apple', ...) will match “/apple”, “/apple/images”, “/apple/images/news”, and so on.

so, suppose I have two router middleware functions:

app.get('/apple', function() {});
app.get('/apple/images', function() {});

and the request is

GET http://domain.com/apple/images

so I want that my second function to process the request, but as I understand the first one will be called as well, correct? Is there any way to skip the first one? I understand that I can call next() from the first function:

app.get('/apple', function(req,res,next) {next()});

but is it really how it should be done? Should I use next('router') in this case?

Upvotes: 0

Views: 53

Answers (1)

Paul
Paul

Reputation: 36349

Order matters. Define the longer path first and you'll get the behavior you want.

Upvotes: 2

Related Questions