From a50091414360bdde6ede19a307cc475302236d25 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 26 Apr 2016 16:59:27 -0400 Subject: [PATCH] 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. --- src/routes/index.js | 6 ++---- src/routes/plugins.js | 11 ++++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/routes/index.js b/src/routes/index.js index 395557ef4b..999528aadf 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -174,10 +174,8 @@ function handle404(app, middleware) { res.type('text/javascript').status(200).send(''); } else if (isLanguage.test(req.url)) { res.status(200).json({}); - } else if (req.path.startsWith(relativePath + '/uploads')) { - res.status(404).send(''); - } else if (req.path === '/favicon.ico') { - res.status(404).send(''); + } else if (req.path.startsWith(relativePath + '/uploads') || req.get('accept').indexOf('text/html') === -1 || req.path === '/favicon.ico') { + res.sendStatus(404); } else if (req.accepts('html')) { if (process.env.NODE_ENV === 'development') { winston.warn('Route requested but not found: ' + req.url); diff --git a/src/routes/plugins.js b/src/routes/plugins.js index 0123433db9..18c928e3af 100644 --- a/src/routes/plugins.js +++ b/src/routes/plugins.js @@ -26,6 +26,15 @@ module.exports = function(app, middleware, controllers) { 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); + } + } + }); }); }; \ No newline at end of file