Is there a way to register an express.js app.get() call with a lower priority?
For example,
app.get('*', function(req, res) {});
app.get('/page', function(req, res) {});
Would there be a way to specify a lower priority on that first call so that it is at the bottom of the route lookup, allowing the later called path to be checked first, effectively as if the first line of code was executed after the second line of code?
By using app. route() method, we can create chainable route handlers for a route path in Express.
In Express if we want to redirect the user to the 404 error page if a particular route does not exist then we can use the app. all() method as the last route handler method and * (asterisk) as the route name.
We can use more than one middleware on an Express app instance, which means that we can use more than one middleware inside app.
The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.
I just had a quick look at Express' source code and it does not seem you can set any kind of priority when you add routes. Routes are always matched by order of creation. In your example, it will first try to match '*'
, then '/page'
.
However, you can ask Express to resume matching after you are done:
app.get('*', function (req, res, next) { // run some tests on the request // ... // finally, resume matching next(); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With