Steps to setup the Node Application
Step 1: Initialize a directory as a new Node.js project
npm init -y
Step 2: Install Express and body-parser
npm install express body-parser
The updated dependencies in package.json file for backend will look like:
"dependencies": {
"body-parser": "^1.20.2",
"express": "^4.18.2",
}
Step 3: Create an Express server and add the following code:
Javascript
const express = require( 'express' ); const bodyParser = require( 'body-parser' ); const app = express(); app.use(bodyParser.json({ limit: '1kb' })); app.post( '/api/data' , (req, res) => { const data = req.body; console.log( 'Received data:' , data); res.status(200).json({ message: 'Data received successfully.' }); }); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`Server is running on http: //localhost:${PORT}`); }); |
Step 4: Run the server
node app.js
Step 5: Test the endpoint with a large payload
You can use tools like Postman or curl to send a POST request with a large JSON payload to the `/api/data` endpoint. The server should now be able to handle larger JSON payloads with the “Request Entity Too Large” error.
Output: Your Express server is now running on http://localhost:3000.
How to fix a 413: request entity too large error ?
In this article, we will learn about an Error request entity too large which typically occurs when the size of the data, being sent to a server, exceeds the maximum limit allowed by the server or the web application in web development when handling file uploads, form submissions, or any other data sent in the request body.
We will discuss the following approaches to resolve this error:
Table of Content
- Middleware Approach
- File Upload Middleware Approach
- Raw Body Parsing Approach