expose the Application::handleRequest method (#950)
* * expose the Application::handleRequest method to extend the context * * minor change the handleRequest comment
This commit is contained in:
parent
85ff544f7a
commit
53a4446123
1 changed files with 16 additions and 5 deletions
|
@ -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.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue