Skip to content

Instantly share code, notes, and snippets.

@Lxxyx
Last active March 15, 2021 11:03
Show Gist options
  • Save Lxxyx/0e35404f7fb6536812fca714ffb26171 to your computer and use it in GitHub Desktop.
Save Lxxyx/0e35404f7fb6536812fca714ffb26171 to your computer and use it in GitHub Desktop.
Hooks 中间件设计
export const config = {
middleware: [
async (next) => {
const ctx = useContext()
ctx.name = ctx.request.body.name
await next()
}
]
}
export default async () => {
const ctx = useContext()
return ctx.name
}
export default withController({
middleware: [
async (next) => {
const ctx = useContext()
ctx.name = ctx.request.body.name
await next()
}
]
}, () => {
const ctx = useContext()
return ctx.name
})
import { hooks, createConfiguration } from '@midwayjs/hooks'
import { Application } from '@midwayjs/koa'
import bodyParser from 'koa-bodyparser'
export default createConfiguration({
imports: [
hooks({
middleware: [
bodyParser(),
async (ctx, next) => {
ctx.name = ctx.request.body.name
await next()
}
]
})
],
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment