expose the Application::handleRequest method (#950)

* * expose the Application::handleRequest method to extend the context

* * minor change the handleRequest comment
master
Riceball LEE 2017-11-06 20:21:52 +08:00 committed by jongleberry
parent 85ff544f7a
commit 53a4446123
1 changed files with 16 additions and 5 deletions

View File

@ -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.
*