Last active
March 15, 2021 11:03
-
-
Save Lxxyx/0e35404f7fb6536812fca714ffb26171 to your computer and use it in GitHub Desktop.
Hooks 中间件设计
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default withController({ | |
middleware: [ | |
async (next) => { | |
const ctx = useContext() | |
ctx.name = ctx.request.body.name | |
await next() | |
} | |
] | |
}, () => { | |
const ctx = useContext() | |
return ctx.name | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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