Skip to content

HTTP – Routes

Bartosz Łaniewski edited this page Mar 24, 2018 · 8 revisions

A router is a class which defines a group of routes under a common namespace. Our framework automatically initialises and populates each imported router. Additionally, we provide several decorators for faster RESTful APIs creation and middleware handling.

import compose from "koa-compose";
import {get, post, patch, del} from "inra-server-http";

class SomeRouter {
  constructor({models, logger, middlewares}) {
      this.models = models;
      this.logger = logger;
  }

  @post("/some/route", function() {
    return compose([
      // `this` is bound to imported middlewares
      this.middlewareA(),
      this.middlewareB()
    ]);
  })
  create(ctx, next) {
      // …
  }

  @get("/some/route/:id")
  read(ctx, next) {
      // …
  }

  @patch("/some/route/:id")
  update(ctx, next) {
      // …
  }

  @del("/some/route/:id")
  delete(ctx, next) {
      // …
  }
}