middleware
Middleware
Psychic has a concept of middleware which piggy-backs off of Koa, enabling you compose in the routing layer as though you were in a Koa app:
// conf/routes.ts
...
r.post('/sign-in', async (ctx, next) => {
ctx.body = { hello: 'world' }
})
This approach bypasses the controller layer, which we don't recommend since it provides such helpful utilities, but can be useful sometimes, say, when integrating with Koa middleware.
Multiple middleware callbacks
In addition to a single middleware callback, Psychic enables you to provide several, ordered middleware callbacks. To do this, simply put the callbacks into an array, like so:
r.post('/sign-in', [
async (ctx, next) => {
console.log('A')
await next()
},
async (ctx, next) => {
console.log('B')
await next()
},
async (ctx, next) => {
console.log('C')
ctx.body = { nowIKnowMy: ['a', 'b', 'c']}
},
)