Skip to content

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

Middleware 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" })
}

Pipe Interface

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 ctx be 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" })
}

Selecting controller interface

TBD

Clone this wiki locally