mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 19:15:58 +01:00 
			
		
		
		
	added new hook 'action:meta.override404' so plugins can override the regular handling of pages that are not found
This commit is contained in:
		| @@ -6,12 +6,19 @@ var nconf = require('nconf'), | |||||||
|  |  | ||||||
| 	translator = require('../../public/src/translator'), | 	translator = require('../../public/src/translator'), | ||||||
| 	categories = require('../categories'), | 	categories = require('../categories'), | ||||||
|  | 	plugins = require('../plugins'), | ||||||
| 	meta = require('../meta'); | 	meta = require('../meta'); | ||||||
|  |  | ||||||
| var helpers = {}; | var helpers = {}; | ||||||
|  |  | ||||||
| helpers.notFound = function(req, res, error) { | 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}); | 		res.status(404).json({path: req.path.replace(/^\/api/, ''), error: error}); | ||||||
| 	} else { | 	} else { | ||||||
| 		res.status(404).render('404', {path: req.path, error: error}); | 		res.status(404).render('404', {path: req.path, error: error}); | ||||||
|   | |||||||
| @@ -173,6 +173,7 @@ module.exports = function(app, middleware) { | |||||||
|  |  | ||||||
| function handle404(app, middleware) { | function handle404(app, middleware) { | ||||||
| 	app.use(function(req, res, next) { | 	app.use(function(req, res, next) { | ||||||
|  | 		if (!plugins.hasListeners('action:meta.override404')) { | ||||||
| 			var relativePath = nconf.get('relative_path'); | 			var relativePath = nconf.get('relative_path'); | ||||||
| 			var	isLanguage = new RegExp('^' + relativePath + '/language/[\\w]{2,}/.*.json'), | 			var	isLanguage = new RegExp('^' + relativePath + '/language/[\\w]{2,}/.*.json'), | ||||||
| 				isClientScript = new RegExp('^' + relativePath + '\\/src\\/.+\\.js'); | 				isClientScript = new RegExp('^' + relativePath + '\\/src\\/.+\\.js'); | ||||||
| @@ -198,6 +199,13 @@ function handle404(app, middleware) { | |||||||
| 			} else { | 			} else { | ||||||
| 				res.status(404).type('txt').send('Not found'); | 				res.status(404).type('txt').send('Not found'); | ||||||
| 			} | 			} | ||||||
|  | 		} else { | ||||||
|  | 			plugins.fireHook('action:meta.override404', { | ||||||
|  | 				req: req, | ||||||
|  | 				res: res, | ||||||
|  | 				error: {} | ||||||
|  | 			}); | ||||||
|  | 		} | ||||||
| 	}); | 	}); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user