Description
Hello!
I'm trying to implement a pretty trivial thing — a catch-all handler that will return 404 when request handler is not found for the request in question, however, I can't find any related examples in the docs and the Internet. What is the best practice for this?
The example below won't work because the router.all('/(.*)')
will intercept all requests after they were handled. Of course I can do something like context.handled = true
and then check for this flag in the catch-all handler, but this will be a boilerplate code that should actually be handled by the router itself. Avoiding a call to next()
in the request handler will also be a mistake, because it will break the middleware that do request post-processing (e.g. response transformation, etc). If I change the order of handler registration then I will need to set proper status in all the request handlers.
router.get('/health', (context, next) => {
context.body = {
status: 'ok',
};
return next();
});
router.all('/(.*)', (context, next) => {
context.status = 404;
context.body = {
message: 'Not found',
};
return next();
});
I believe the router should provide a special method for handling unhandled requests.