How to Fix "413 Request Entity Too Large" Error in Node.js
Tags: javascript, node, programming, softwaredevelopment
Published: 2024-01-19 21:27:03

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](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/413), 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`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) header field.

HTTP `413 Payload Too Large` was [previously called](https://www.rfc-editor.org/rfc/rfc2616#section-10.4.14) [`413 Request Entity Too Large`](https://www.rfc-editor.org/rfc/rfc2616#section-10.4.14). 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](http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size). 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](https://writech.run/blog/how-to-fix-error-413-in-apps-behind-nginx-on-aws-elastic-beanstalk-c68a1ef2c331).

## 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:

```javascript
// 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}))

// ...
```

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](https://writech.run/blog/how-to-fix-413-request-entity-too-large-error-in-node-js-226c0070e4f4/)" appeared first on [Writech](https://writech.run)._