[英]Nest.js - request entity too large PayloadTooLargeError: request entity too large
我正在嘗試將JSON
保存到 Nest.js 服務器中,但是當我嘗試這樣做時服務器崩潰了,這就是我在 console.log 上看到的問題:
[Nest] 1976 - 2018-10-12 09:52:04 [ExceptionsHandler] request entity too large PayloadTooLargeError: request entity too large
一件事是 JSON 請求的大小是 1095922 字節,有人知道 Nest.js 中如何增加有效請求的大小嗎? 謝謝!
我找到了解決辦法,因為這個問題表達(Nest.js用途表達背后的場景),我發現在這個線程解決方案相關的錯誤:請求實體太大,我所做的就是修改main.ts
文件中添加body-parser
依賴項並添加一些新配置以增加JSON
請求的大小,然后我使用文件中可用的app
實例來應用這些更改。
import { NestFactory } from '@nestjs/core';
import * as bodyParser from 'body-parser';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useStaticAssets(`${__dirname}/public`);
// the next two lines did the trick
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
app.enableCors();
await app.listen(3001);
}
bootstrap();
您還可以從 express 導入urlencoded
和json
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { urlencoded, json } from 'express';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('api');
app.use(json({ limit: '50mb' }));
app.use(urlencoded({ extended: true, limit: '50mb' }));
await app.listen(process.env.PORT || 3000);
}
bootstrap();
為我解決的解決方案是增加 bodyLimit。 來源: https : //www.fastify.io/docs/latest/Server/#bodylimit
const app = await NestFactory.create<NestFastifyApplication>(
AppModule,
new FastifyAdapter({ bodyLimit: 10048576 }),
body-parser 定義的默認限制是 100kb: https : //github.com/expressjs/body-parser/blob/0632e2f378d53579b6b2e4402258f4406e62ac6f/lib/types/json.js#L53-L55
希望這可以幫助 :)
對我來說它有幫助,我將 100kb 設置為 50mb
這解決了我的問題,並且在 nestjs bodyparser 中現在已經貶值,所以這可能是一個合適的解決方案。
app.use(express.json({limit: '50mb'}));
app.use(express.urlencoded({limit: '50mb'}));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.