-
Notifications
You must be signed in to change notification settings - Fork 123
Controllers Interface
osher edited this page Nov 27, 2016
·
9 revisions
A user can chose one of two styles for the interface of web-operation methods:
- middleware interface
- pipe interface
This is the interface the project initially launched with. It is the default interface for same reason.
With this style, user controllers should look like this:
module.exports = {
myOperation
}
function myOperation(req, res, next) {
const params = req.swagger.params;
const poperations = req.swagger.operation;
//do your logic here
res.setHeader("x-foo", "bar" );
next(null, { message: "Hello World" })
}
This alternative to the middleware interface comes to increase reuse and testability. It persues the following goals (not all of which are fully accomplished, but are under work):
- let the
ctxbe a basis for dependency injection - isolate the handlers from web-context, letting you reuse the same code in CLI or message-queue consumer
As of the time of writing this page, only the first step was taken: isolate from ctx.response.
Better isolation from ctx.request is still in design/progress.
With this style, user controllers should look like this:
module.exports = {
myOperation
}
function myOperation(ctx, next) {
const params = ctx.request.swagger.params; //swagger will be available on the ctx in the future
const poperations = ctx.request.swagger.operation;//swagger will be available on the ctx in the future
//do your logic here
ctx.headers["x-foo"] = "bar";
next(null, { message: "Hello World" })
}
TBD