Skip to content

Commit

Permalink
expose the Application::handleRequest method (#950)
Browse files Browse the repository at this point in the history
* * expose the Application::handleRequest method to extend the context

* * minor change the handleRequest comment
  • Loading branch information
snowyu authored and jonathanong committed Nov 6, 2017
1 parent 85ff544 commit 53a4446
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/application.js
Expand Up @@ -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.
*
Expand Down

0 comments on commit 53a4446

Please sign in to comment.