404 returns HTML template instead JSON response.
See "How do I handle 404 responses?" at the expressjs.com FAQ.
They state that 404 simply indicates an absence of additional work to do (i.e., none of the middleware functions responded). Changing the default behavior can be accomplished by installing a middleware function at the very end of the stack:
app.use(function (req, res, next) {
res.status(404).json(error: 'not found');
})
The problem is that SakuraApi doesn't give a convenient way to add middleware to "the very end of the stack". A feature similar to addLastErrorHandlers(...) needs to be added after all the other middleware and routes have been added. The best place is probably here.
Proposal
Proposal 1: .addFinalMiddleware
This would be similar to .addMiddleware, but it's map of handlers would be added at the end of the stack.
Use of this would look something like:
const sapi = new SakuraApi(...);
// add some middleware things here
sapi.addMiddleware((req, res, next) => next());
sapi.addMiddleware((req, res, next) => next());
// add stuff to the "end of the stack"
sapi.addFinalMiddleware((req, res, next) => req.send(404));
The only problem with this approach is that the integrator needs to implement the 404 handler himself, otherwise Express will resort to sending an html body with its 404.
Perhaps this could be overcome by simply adding a default 404 handler at the end unless the integrator calls sapi.addFinalMiddleware in which case the default 404 handler is removed and the integrator is now responsible for adding whatever final middleware he wants, the last of which should be the 404 handler.
Proposal 2: add an onInsertFinalMiddleware hook
See onAuthenticationError for example.
The method signature might be something like: onInsertFinalMiddleware: (app: Express) => void.
It could have a default value:
export class SakuraApi {
// ... somewhere near the top... by the other hooks...
onInsertFinalMiddleware = (app: Express) => app.use((req, res) => res.send(404).json(error:'NOT_FOUND')));
// ... rest of the SakuraApi class
}
This hook would be called at the end of the stack just like in proposal 1.
If the integrator provides his own alternative to onInsertFinalMiddleware, just like in proposal 1, he'd be responsible for re-implementing his own 404 handler after he inserted his other final middleware handlers.
An advantage to proposal 2 is that it would possibly allow #140 to be addressed since the integrator could implement something like:
const sapi = new SakuraApi(...);
sapi.onInsertFinalMiddleware((app: Express) => {
app.use('*', (req, res, next) => {
// interceptor stuff
next();
});
app.use((req, res) => res.status(404).json({error: 'NOT_FOUND'});
});
Proposal: 3 have a dedicated 404 hook
Perhaps something like on404. This hook would have a default handler assigned to it that returns a 404 json error response. This handler would be called at the very end of the stack as proposed above.
In addition to this, either proposal 1 or 2 would be implemented, allow other final middleware prior to the on404 handler being called.
My preference
Proposal 2 & 3, which combined would also solve #140 and would make it so that you could implement final middleware handlers without having to re-implement the standard 404 behavior if you're fine with the default json 404 behavior of SakuraApi.
A note of caution:
It may not be as simple as adding a handler to the end of the stack. If handlers are passing off to the next handler, they may not be sensitive to the fact that they are the last handler in the stack, in which case 404 will get called. A hack around this is to check if the headers have been set. For example:
app.use('/endpoint', (req, res, next) => {
res.json({
result: 1
});
next();
});
app.use((req, res) => {
if (!res.headersSent)
res.status(404).json({error: 'NOT_FOUND'});
});
Re: A problem with proposal 2 & questioning if we really want to implement this ticket:
Proposal 2 doesn't actually add the value that it seems to since the interceptor would have to be added to the middleware before the route. See #140
As noted above, Express's prescribed approach to implementing a custom 404 handler is to just add a handler to the bottom of the stack. This, however, introduces problems since we need to assume that no handler calls next if it's supposed to be the last handler (which means it needs to know it's the last handler). This gets complicated because SakuraApi has after and afterAll hooks which allow handlers to be added before and after a route, but keeping tracking of which one was the last one might not always be convenient. If we don't implement this ticket, then it doesn't matter... you can call next in any handler and it won't cause a problem. On the other hand if we do implement custom 404 handling capabilities, then we have to do hacky things like checking to see if res.headersSent is true or we end up with weird behaviors if we implement #140 -- for example.
Proposal 4: Do nothing
The client should recognize a 404 as an error status, which should be handled differently than a standard 200 response. If the client detects a 404, simply do not parse the body. This might cause an issue however since the implementor might choose to send a 404 if a valid endpoint is called but a resource is not available. In this case, the client should check the content-type to determine if the 404 is json first. Arguably, however, the implementor might choose to send a 200 with an empty body. It's kind of hard to know because this is really up to the implementor and shouldn't be something that SakuraApi is opinionated about.