Middleware

Enso ships with commonly used middleware to reduce the pain of finding the correct package šŸ˜“

Please make some suggestions if you feel that any of these packages should not be included. For example Iā€™m undecided if koa-morgan should be included given winston is also popular logging tool.

We have also normalise the names to make life easier.

Packaged middlware

The following middleware comes packaged and recommended from Enso.

import { bodyparser } from '@ensojs/framework'
import { helmet } from '@ensojs/framework'
import { jwt } from '@ensojs/framework'
import { cors } from '@ensojs/framework'
import { multer } from '@ensojs/framework'
import { morgan } from '@ensojs/framework'

Global middleware

Global middleware is defined within the applyMiddleware method when you define your App

// file: src/App.ts
export class App extends HTTPServer {

  applyMiddleware (koa, container: Container): void {
    koa.use(helmet())
    koa.use(bodyparser())
    koa.use(cors())
    koa.use(morgan('combined'))
    koa.use(morgan('dev'))
  }

}

Controller middleware

Middleware can be applied to a controller @controller(path, middleware?) decorator.

Endpoint middleware

Middleware can be applied to an endpoint through a HTTP decorator. Refer back to the routing docs.

Next

  • Learn how we organise our Domain in Enso.