diff --git a/lib/application.js b/lib/application.js index a013c96..5e09e8a 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. *