Updated 404 for image handling

If an image or asset specified in static file handler is not
found (ENOENT), then the 404 handler is now invoked (as opposed to
prior, where a handled exception was thrown).

Also, when requesting images inline that do not exist, NodeBB
will now send back "404 Not Found" instead of the entire 404
page. If you access the broken link directly, you'll see the
404 page.
v1.18.x
Julian Lam 9 years ago
parent ae927ce356
commit a500914143

@ -174,10 +174,8 @@ function handle404(app, middleware) {
res.type('text/javascript').status(200).send(''); res.type('text/javascript').status(200).send('');
} else if (isLanguage.test(req.url)) { } else if (isLanguage.test(req.url)) {
res.status(200).json({}); res.status(200).json({});
} else if (req.path.startsWith(relativePath + '/uploads')) { } else if (req.path.startsWith(relativePath + '/uploads') || req.get('accept').indexOf('text/html') === -1 || req.path === '/favicon.ico') {
res.status(404).send(''); res.sendStatus(404);
} else if (req.path === '/favicon.ico') {
res.status(404).send('');
} else if (req.accepts('html')) { } else if (req.accepts('html')) {
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
winston.warn('Route requested but not found: ' + req.url); winston.warn('Route requested but not found: ' + req.url);

@ -26,6 +26,15 @@ module.exports = function(app, middleware, controllers) {
return next(); return next();
} }
res.sendFile(matches[0]); res.sendFile(matches[0], {}, function(err) {
if (err) {
if (err.code === 'ENOENT') {
// File doesn't exist, this isn't an error, to send to 404 handler
return next();
} else {
return next(err);
}
}
});
}); });
}; };
Loading…
Cancel
Save