From f14c5f7e1ced024ebe45ed2795173fba03a5e08f Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 6 Mar 2015 13:40:35 -0500 Subject: [PATCH] added new hook 'action:meta.override404' so plugins can override the regular handling of pages that are not found --- src/controllers/helpers.js | 9 ++++++- src/routes/index.js | 52 ++++++++++++++++++++++---------------- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js index 7958ace2df..dd5961839b 100644 --- a/src/controllers/helpers.js +++ b/src/controllers/helpers.js @@ -6,12 +6,19 @@ var nconf = require('nconf'), translator = require('../../public/src/translator'), categories = require('../categories'), + plugins = require('../plugins'), meta = require('../meta'); var helpers = {}; helpers.notFound = function(req, res, error) { - if (res.locals.isAPI) { + if (plugins.hasListeners('action:meta.override404')) { + plugins.fireHook('action:meta.override404', { + req: req, + res: res, + error: error + }); + } else if (res.locals.isAPI) { res.status(404).json({path: req.path.replace(/^\/api/, ''), error: error}); } else { res.status(404).render('404', {path: req.path, error: error}); diff --git a/src/routes/index.js b/src/routes/index.js index 0b951197c5..a6c51b17c9 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -173,30 +173,38 @@ module.exports = function(app, middleware) { function handle404(app, middleware) { app.use(function(req, res, next) { - var relativePath = nconf.get('relative_path'); - var isLanguage = new RegExp('^' + relativePath + '/language/[\\w]{2,}/.*.json'), - isClientScript = new RegExp('^' + relativePath + '\\/src\\/.+\\.js'); - - if (isClientScript.test(req.url)) { - res.type('text/javascript').status(200).send(''); - } else if (isLanguage.test(req.url)) { - res.status(200).json({}); - } else if (req.accepts('html')) { - if (process.env.NODE_ENV === 'development') { - winston.warn('Route requested but not found: ' + req.url); + if (!plugins.hasListeners('action:meta.override404')) { + var relativePath = nconf.get('relative_path'); + var isLanguage = new RegExp('^' + relativePath + '/language/[\\w]{2,}/.*.json'), + isClientScript = new RegExp('^' + relativePath + '\\/src\\/.+\\.js'); + + if (isClientScript.test(req.url)) { + res.type('text/javascript').status(200).send(''); + } else if (isLanguage.test(req.url)) { + res.status(200).json({}); + } else if (req.accepts('html')) { + if (process.env.NODE_ENV === 'development') { + winston.warn('Route requested but not found: ' + req.url); + } + + res.status(404); + + if (res.locals.isAPI) { + return res.json({path: req.path, error: 'not-found'}); + } + + middleware.buildHeader(req, res, function() { + res.render('404', {path: req.path}); + }); + } else { + res.status(404).type('txt').send('Not found'); } - - res.status(404); - - if (res.locals.isAPI) { - return res.json({path: req.path, error: 'not-found'}); - } - - middleware.buildHeader(req, res, function() { - res.render('404', {path: req.path}); - }); } else { - res.status(404).type('txt').send('Not found'); + plugins.fireHook('action:meta.override404', { + req: req, + res: res, + error: {} + }); } }); }