Sling Academy
Home/Node.js/Fixing Node.js & Express Error: Request Entity Too Large

Fixing Node.js & Express Error: Request Entity Too Large

Last updated: December 28, 2023

When developing with Node.js and Express, you might encounter an error stating ‘request entity too large’. This error occurs when a client sends a request to the server with a body payload that exceeds the maximum limit the server is willing to accept.

Understanding the Error

This limit is in place to protect your server against potential denial-of-service (DoS) attacks, which could be caused by very large payloads. By default, Express uses a middleware called body-parser with a limit of 100kb. If the size of the request body exceeds this, the error is issued.

How to Fix the Error

1. Increasing the Payload Size Limit

To fix this error, you can increase the payload size limit using the body-parser middleware:

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

const app = express();

// Increase the payload size limit, e.g., to 50mb
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ 
    limit: '50mb', 
    extended: true 
}));

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

2. Using Express’s Built-In Middleware

In newer versions of Express (`^4.16.0`), you can directly set the limit using the built-in middleware without separately including body-parser:

const express = require('express');
const app = express();

// Set limit to 50mb for JSON and URL-encoded bodies
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ 
    limit: '50mb', 
    extended: true 
}));

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

3. Handling the Error Gracefully

If you prefer not to increase the payload limit for security reasons, you can handle the error more gracefully and provide a meaningful response to the client:

const express = require('express');
const app = express();

// Set JSON payload limit to 100kb
app.use(express.json({ limit: '100kb' }));

// Error handling middleware
app.use((err, req, res, next) => {
    if (err.status === 413) {
        return res.status(413).json({ message: 'Payload too large' });
    }
    next(err);
});

// Start the server
app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

These fixes should help resolve the ‘request entity too large’ error in a Node.js and Express application, improving your system’s robustness and the user experience.

Next Article: Express.js Error: Router.use() requires a middleware function but got a Object

Previous Article: Node.js Error: read ECONNRESET [Solved]

Series: Dealing with Common Errors in Node.js

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates