【问题标题】:Nest.js raw bodyParser doesn't fire json functionNest.js 原始 bodyParser 不会触发 json 函数
【发布时间】:2022-02-08 06:14:37
【问题描述】:

我需要获取原始正文请求以生成签名。 当我找到解决方案时,我遇到了 json 函数不起作用的问题 下面的例子:

import { Injectable, NestMiddleware } from '@nestjs/common';
import { json } from 'body-parser';
import { IncomingMessage } from 'http';

@Injectable()
export class RawBodyMiddlewareMiddleware implements NestMiddleware {
  public use(req: IncomingMessage, res, next: () => any): any {
    json({ //json function not fired
      verify: (req, res, buffer) => {
        console.log(buffer);
        if (Buffer.isBuffer(buffer)) {
          const rawBody = Buffer.from(buffer);
          req['parsedRawBody'] = rawBody;
        }
        return true;
      },
    })(req, res as any, next);
  }
}

【问题讨论】:

    标签: nestjs body-parser


    【解决方案1】:

    经过调查,我了解到出于某种原因我需要将 bodyParser 标记为 false。

    const app = await NestFactory.create(AppModule, {
        bodyParser: false,
      });
    
    

    【讨论】:

    • 这将禁用默认的正文解析器。所以你可以使用你定制的
    猜你喜欢
    • 2014-06-28
    • 2023-03-14
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 2016-10-25
    • 2011-11-23
    • 2015-03-29
    相关资源
    最近更新 更多