【问题标题】:NestJS/ExpressJs too many parametersNestJS/ExpressJs 参数太多
【发布时间】:2018-01-25 23:28:42
【问题描述】:

我正在尝试将文件发送到 NestJS 控制器,但不断收到 too many parameters 异常。我已经安装了bodyParser 并更新了请求大小限制以绕过request too large 异常。

main.ts:

import { NestFactory } from "@nestjs/core";
import { ApplicationModule } from "./app/app.module";
import * as express from "express";
import * as bodyParser from "body-parser";

async function bootstrap() {
    const server = express();
    server.use(bodyParser({limit: '50mb'}));

    console.log(server);

    const app = await NestFactory.create(ApplicationModule, server);
    await app.listen(process.env.PORT || 3000);
}
bootstrap();

控制器:

import { Get, Controller, Query, Post, Request } from "@nestjs/common";
import { CloudVisionLogoService } from "./logos.component";

@Controller("logos")
export class LogoRecognitionController {
    public constructor(
        private readonly _logoRecognition: CloudVisionLogoService
    ) {
    }

    @Post()
    public async detectLogos(@Request() req) {
        console.log(req.files[0]);
        // return await this._logoRecognition.detectLogos(imageUri);
    }
}

邮递员请求(未显示,图片的二进制附件):

POST /logos HTTP/1.1
Host: localhost:3000
Content-Type: application/x-www-form-urlencoded
Cache-Control: no-cache
Postman-Token: c95da069-c602-58a9-1e05-36456a527f02

undefined

【问题讨论】:

    标签: node.js express nestjs


    【解决方案1】:

    以下来自body-parser docs:

    这不处理多部分实体,因为它们复杂且 通常大的性质。对于多部分实体,您可能对 以下模块:

    busboy 和 connect-busboy

    多方和连接多方

    厉害

    混合器

    我建议你使用multer 包,因为它很简单,很多用户都在使用它

    【讨论】:

    • 这很有帮助。此外,Postman 自动将我的内容类型设置为“application/x-www-form-urlencoded”,这在我切换到 multer 后继续破坏我的代码。
    猜你喜欢
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 2020-09-05
    • 2018-02-06
    相关资源
    最近更新 更多