Just to close the loop on this - I didn’t want to go on the server side rendering track, so I applied the following in my middleware error handler:
if (err.name === 'UnauthorizedError') { res.status(401).send(prepareErrorResponse(isXhr, 'Acess Denied')); } else if (err.status === 404) { res.status(404).send(prepareErrorResponse(isXhr, 'Not Found')); if (req.accepts('html')) { // Respond with html page. fs.readFile(__dirname + '/../404.html'), 'utf-8', function(err, page) { res.writeHead(404, {'Content-Type': 'text/html'}); res.write(page); res.end(); }); } else { if (req.accepts('json')) { // Respond with json. res.status(404).send({ error: 'Not found' }); } else { // Default to plain-text. send() res.status(404).type('txt').send('Not found'); } } }
Basically, instead of showing the NotFound component on the client (with soft 404), I serve the NotFound page directly from the server (with status 404).
source share