I need to send the request which contains a base64 string and it's too large (probable, around 2 mb).
But server throw next exeption, how to avoid this error:
[Nest] 1666 - 11/01/2021, 1:50:58 PM ERROR [ExceptionsHandler] request entity too large
gateway-client | PayloadTooLargeError: request entity too large
gateway-client | at readStream (/srv/app/node_modules/raw-body/index.js:155:17)
gateway-client | at getRawBody (/srv/app/node_modules/raw-body/index.js:108:12)
gateway-client | at read (/srv/app/node_modules/body-parser/lib/read.js:77:3)
gateway-client | at jsonParser (/srv/app/node_modules/body-parser/lib/types/json.js:135:5)
gateway-client | at Layer.handle [as handle_request] (/srv/app/node_modules/express/lib/router/layer.js:95:5)
gateway-client | at trim_prefix (/srv/app/node_modules/express/lib/router/index.js:317:13)
gateway-client | at /srv/app/node_modules/express/lib/router/index.js:284:7
gateway-client | at Function.process_params (/srv/app/node_modules/express/lib/router/index.js:335:12)
gateway-client | at next (/srv/app/node_modules/express/lib/router/index.js:275:10)
gateway-client | at expressInit (/srv/app/node_modules/express/lib/middleware/init.js:40:5)
My main.ts includes:
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
// app.enableCors({
// origin: '*',
// });
const config = new DocumentBuilder()
.setTitle('API client gateway')
.setDescription('API client gateway full documentation')
.setVersion('v0.0.1')
.addTag('')
.addBearerAuth(
{
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
name: 'JWT',
description: 'Enter JWT token',
in: 'header',
},
'jwt-token',
)
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('/api/docs', app, document);
await app.init();
app.use(express.json({limit: "50mb"})) //For JSON requests
app.use(express.urlencoded({
limit: "50mb",
extended: false
}));
await app.listen(AppConfig.Port);
}
bootstrap();
CodePudding user response:
In your main.ts
file (most probably bootstrap()
method) get instance of Express App and set JSON parser with limit:
import { json } from 'body-parser';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule,
{ bodyParser: false });
app.use(json({ limit: '5mb' }));
...
}
EDIT: If you are accessing the app via proxy (nginx for example) make sure there is the same limit allowed as well.