Saturday, October 30, 2021

How to fix Node.js PayloadTooLargeError: request entity too large

Displaying results on a visualization when there were many areas that needed to be displayed on a map caused a HTTP 413 PayloadTooLarge error.  The HTTP request in question had 20,000+ areas defined that made a request payload of 150+ kb.

Things fixed:

The first problem was that Node's HTTP request body parser default request payload limit is 100kb. I increased it to 1mb, as I don't want to arbitrarily make it any bigger. NOTE: nginx didn't have any request payload limits configured.

The second thing was that since Node express version 4.16.0, no longer needed a separate body-parser library, I could use express's internal one.

OLD for Node 8.11.3

const express = require("express");
const bodyParser = require("body-parser");

const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

NEW for Node 14.17.1

const express = require("express");

const app = express();
// The default express max request limit is 100kb, increase it
const maxRequestBodySize = '1mb';
app.use(express.json({limit: maxRequestBodySize}));
app.use(express.urlencoded({limit: maxRequestBodySize}));

Helpful Articles:

https://stackoverflow.com/questions/19917401/error-request-entity-too-large

https://stackoverflow.com/questions/31967138/node-js-express-js-bodyparser-post-limit











No comments:

Post a Comment

I appreciate your time in leaving a comment!