DEV Community

Cover image for How to Fix "413 Request Entity Too Large" Error in Node.js
Antonello Zanini for Writech

Posted on • Originally published at writech.run

How to Fix "413 Request Entity Too Large" Error in Node.js

I recently had to deal with the "413 Request Entity Too Large" Express error. After some research, I found out that this is a fairly common problem that can occur for two general reasons. In both cases, the error is thrown by the server when a client makes a request that is considered too large.

Let's now learn everything you need to know about the "413 Request Entity Too Large" error and see how to fix it in Express.

What Is the "413 Request Entity Too Large" Error?

As explained in MDN, the HTTP 413 Payload Too Large response status code indicates that the request entity performed by the client is larger than the limits defined by the server. As a result of this, the server might close the connection or return a Retry-After header field.

HTTP 413 Payload Too Large was previously called 413 Request Entity Too Large. Note that most systems still call it that way. Therefore, if you come across a "Request Entity Too Large" error message, you can assume you are dealing with an HTTP 413 Payload Too Large error.

Why Does the HTTP Error 413 Occur?

As explained earlier, an HTTP error 413 occurs when the client request is larger than the server is willing or able to process. Specifically, this may happen because of two reasons. Let's dig into them.

1. Poorly Configured Web Server

You get an HTTP error 413 because the client request exceeds the size limit defined in your web server configuration. For example, the client request size limit imposed by Nginx by default is set to 1 MB. Therefore, if you do not configure Nginx to explicitly expect a larger value, you will get an HTTP error 413 for any request larger than 1MB.

2. Poorly Configured Application Server

You get an HTTP error 413 because the client request exceeds the size limit defined in your application server configuration. For example, an Express application on a Node.js server limits the JSON request body to 100Kb by default. Therefore, if you do not configure Express to explicitly expect a larger value, you will get an HTTP error 413 for any JSON request larger than 100Kb.

In this article, you will learn how to address HTPP 413 errors caused by the second cause, namely a badly configured application server. If you want to delve into the second cause, check out this article.

Fixing "413 Request Entity Too Large" Error in Express

To fix the "413 Request Entity Too Large" error in an Express application, you only have to update the server initialization file. This is generally called index.js or server.js and is where you initialize the Express server. Make sure this file contains the following lines:

// index.js or server.js

const express = require("express")

// ...

const app = express()

// ...

// fixing "413 Request Entity Too Large" errors
app.use(express.json({limit: "10mb", extended: true}))
app.use(express.urlencoded({limit: "10mb", extended: true, parameterLimit: 50000}))

// ...
Enter fullscreen mode Exit fullscreen mode

These last two lines of code extend the size of the JSON payload and URL query parameters to 10MB. This should be sufficient to avoid most of the "413 Request Entity Too Large" errors on your Express application. Configure the two limit values according to your needs, but without unnecessarily oversizing them.

Et voilà! You just learned how to solve the "413 Request Entity Too Large" error on a Node.js Express application.

Conclusion

In this article, you learned what an HTTP error 413 is, why it occurs, and how to address it. Specifically, you saw how to fix the "413 Request Entity Too Large" on a Node.js Express server. This is a particularly common problem that occurs when the client request is larger than the server expects. As shown, it only takes a couple of lines of code to solve it.

Thanks for reading! I hope that you found this article helpful.


The post "How to Fix "413 Request Entity Too Large" Error in Node.js" appeared first on Writech.

Top comments (0)