by Sai gowtham

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

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
26,545 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
221,520 students enrolled
44 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
114,575 students enrolled
21 hours of video content
View Course