midwayjs / hooks

"Zero" Api / Type Safe / Fullstack Kit / Powerful Backend
MIT License
690 stars 63 forks source link

更新到3.1.6后,在中间件中调用ctx.request.body为undefined #529

Open dwyanewang opened 1 year ago

dwyanewang commented 1 year ago

更新到3.1.6后,在中间件中调用ctx.request.body为undefined 项目源码地址:https://github.com/dwyanewang/midwayjs-test

复现流程

  1. 下载官方示例
    npx degit https://github.com/midwayjs/hooks/examples/api-bundle ./hooks-app
  2. 新增test中间件
    
    import {useContext} from '@midwayjs/hooks'
    import {Context} from '@midwayjs/koa'

export default async (next: any) => { const ctx = useContext() console.log(111, ctx.request.body)

await next()

console.log(222, ctx.request.body) }

3. 在configuration.ts中引入
```typescript
import {createConfiguration, hooks} from '@midwayjs/hooks'
import {MidwayConfig} from '@midwayjs/core'
import * as Koa from '@midwayjs/koa'
import Test from './test'

export default createConfiguration({
  imports: [Koa, hooks({middleware: [Test]})],
  importConfigs: [
    {
      default: {
        keys: 'session_keys',
        koa: {
          port: 7002,
        },
      } as MidwayConfig,
    },
  ],
})
  1. 将hooks版本改为3.1.6 image image
  2. 在index.ts中新增test接口

    export const test = Api(Post('/api/test'), async () => {
    const ctx = useContext()
    
    return {
    message: 'Hello World!',
    ip: ctx.ip,
    }
    })
  3. 然后运行项目,测试test接口 image image
  4. 更改hooks版本为3.0.1 image image
  5. 然后运行项目,测试test接口 image image
dwyanewang commented 1 year ago

是什么问题可以先说明下吗