From 53a44461230199fd175a9f1c77ba66c7943bfaf5 Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Mon, 6 Nov 2017 20:21:52 +0800 Subject: [PATCH] expose the Application::handleRequest method (#950) * * expose the Application::handleRequest method to extend the context * * minor change the handleRequest comment --- lib/application.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/application.js b/lib/application.js index a013c964c..5e09e8a8b 100644 --- a/lib/application.js +++ b/lib/application.js @@ -128,17 +128,28 @@ module.exports = class Application extends Emitter { if (!this.listeners('error').length) this.on('error', this.onerror); const handleRequest = (req, res) => { - res.statusCode = 404; const ctx = this.createContext(req, res); - const onerror = err => ctx.onerror(err); - const handleResponse = () => respond(ctx); - onFinished(res, onerror); - return fn(ctx).then(handleResponse).catch(onerror); + return this.handleRequest(ctx, fn); }; return handleRequest; } + /** + * Handle request in callback. + * + * @api private + */ + + handleRequest(ctx, fnMiddleware) { + const res = ctx.res; + res.statusCode = 404; + const onerror = err => ctx.onerror(err); + const handleResponse = () => respond(ctx); + onFinished(res, onerror); + return fnMiddleware(ctx).then(handleResponse).catch(onerror); + } + /** * Initialize a new context. *