Last updated: Apr 5, 2024
Reading time·3 min
client_max_body_size
variable if you use NginxTo solve the error "413: request entity too large" in Node.js and Express:
body-parser
module.parameterLimit
property to increase the maximum number of
parameters that are allowed in the URL-encoded data.import express from 'express'; import bodyParser from 'body-parser'; const app = express(); app.use(bodyParser.json({limit: '35mb'})); app.use( bodyParser.urlencoded({ extended: true, limit: '35mb', parameterLimit: 50000, }), );
Make sure that the two app.use()
calls are added before you define any routes.
The code sample above uses the ES6 modules import/export syntax.
If you use the CommonJS require()
syntax, use the following import statements
instead.
const express = require('express'); const bodyParse = require('body-parser')
The limit property is used to control the maximum request body size.
By default, the limit
property is set to 100kb
, so the "request entity too
large" error is raised if the user tries to upload a file that is greater than
100 kilobytes.
import express from 'express'; import bodyParser from 'body-parser'; const app = express(); app.use(bodyParser.json({limit: '35mb'})); app.use( bodyParser.urlencoded({ extended: true, limit: '35mb', parameterLimit: 50000, }), );
You can set the property to a value that suits your use case, e.g. 5mb
or
10mb
.
The parameterLimit property is used to control the maximum number of parameters that are allowed in the URL-encoded data.
If a request contains more parameters than the value of parameterLimit
, then a
"413: request entity too large" error is raised.
By default, the property is set to 1000
.
Also, make sure you have the body-parser module installed.
# If you need to generate a package.json file npm init -y # with NPM npm install express body-parser # or with YARN yarn add express body-parser
client_max_body_size
variable if you use NginxIf you use Nginx in front of your Express.js server and the error persists, add
the following line to your Nginx configuration file, e.g.
/etc/nginx/sites-available
or /etc/nginx/nginx.conf
.
You can try to search for the client_max_body_size
variable to see if it's
defined and you can update its value to increase the max request body size.
# 80MB request body max client_max_body_size 80M;
Your Nginx configuration files should be located in the /etc/nginx/
directory.
The default request body max size (client_max_body_size
) in Nginx is 1
megabyte.
If the request body size exceeds the value of your client_max_body_size
setting, then the "413::Request Entity is too large" exception is raised.
Restart your Nginx server after making the change.
sudo systemctl restart nginx # or sudo service nginx restart
Once you increase the max request body size in the configuration of the
body-parser
module and in your Nginx config, the issue will be resolved.
You can learn more about the related topics by checking out the following tutorials: