decorator based api router


1.0.0-rc.202 years ago5 years agoMinified + gzip package size for @ingress/router in KB



Decorator based router middleware for ingress

npm i @ingress/router

Define routes using the @Route decorator, and plain javascript classes.

Route Definition

import { Route, Param } from '@ingress/router'

export class MyController {

  someRouteHandler (@Param.Path('echo') echo) {
    return 'Echoing ' + echo
// GET /prefix/echo/something ===> something

Getting started

import { Server, DefaultMiddleware } from 'ingress'
import { Router } from '@ingress/router'
import { MyController } from './my-controller'

const app = new Server()

  .use(new DefaultMiddleware())
  .use(new Router({ controllers: [MyController] }))
  .then(() => console.log('started'))


Router (routerOptions): class

  • RouterOptions<T>: object
    • controllers: Array<Type> (required)
    • baseUrl: string
      • Define the base url. Defaults to '/'
    • resolveController<C>(context: T, controller: Type<C>): C
      • resolve the controller instance. By default it return the result of new C()
      • If used in conjunction with @ingress/di The controller will be requested from the current context.scope injector
    • isRoutable: (def: RouteMeatdata) => boolean
      • Identify a class method as routable. Defaults to methods with a @Route decorator
    • getMethods: (def: RouteMetdata) => string[]
      • Identify the http methods for the route. Defaults to methods defined with a @Route decorator
    • getPath: (baseUrl: string, def: RouteMetadata) => string
      • Identify the path for the route. Defaults to paths defined with a @Route decorator

Route : (route: string, ...methods: string[]) => ClassDecorator | MethodDecorator

  • Create a decorator that defines a route with a path and method(s)
    • Additional helper methods:
      • Route.Get
      • Route.Post
      • Route.Put
      • Route.Delete
      • Route.Patch
  • The @Route decorator can be used on classes and class methods.
    • The default behavior will concatenate parent-child (class-method) declarations
  • It can accept additional Route methods, or strings, as rest parameters.
    • @Route('/some/route', Route.Post, 'Put')

Defining middleware

Arbirary middleware can be defined on any handler using the reflection property 'annotations'.

import { createAnnotationFactory } from 'reflect-annotations'

class FancyAnnotation {
  constructor (fancyOrNot) {
    this.fancy = fancyOrNot
  middleware () {
    return (context, next) => {
      context.isFancy = this.fancyOrNot

export const Fancy = createAnnotationFactory(FancyAnnotation)

Using @Fancy(true) on any handler or controller will add the middleware function to the execution of the desired handler or handler(s) respectively.

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.