How to fix the request entity too large error in Express
In this tutorial, we will learn about how to resolve the request entity too large error in express.
In express, we use the body-parser
middleware to parse the data from the incoming requests, and also it sets the default request body size to 100kb
so when a request body size exceeds 100kb
we will see this error.
PayloadTooLargeError: request entity too large
at readStream (/root/server/node_modules/raw-body/index.js:155:17)
at getRawBody (/root/server/node_modules/raw-body/index.js:108:12)
at read (/root/server/node_modules/body-parser/lib/read.js:77:3)
at urlencodedParser (/root/server/node_modules/body-parser/lib/types/urlencoded.js:116:5)
at Layer.handle [as handle_request] (/root/server/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/root/server/node_modules/express/lib/router/index.js:317:13)
at /root/server/node_modules/express/lib/router/index.js:284:7
To fix this error, we need to increase the body-parser size limit like this.
app.use(express.json({limit: '25mb'}));
app.use(express.urlencoded({limit: '25mb'}));
If you are using NGNIX to host your app, you need to add the following line to /etc/nginx/sites-available
directory and restart the server.
client_max_body_size 25M; #25mb