EN
Express.js - limit request JSON size
3
points
In this article, we would like to show you how to limit JSON size in HTTP request while working with Express.js.
Quick solution:
app.use(express.json({ limit: '100kb' }));
Where: 100kb can be changed to uther value units are: b, kb, mb, etc.
Practical example
In this example, we present a simple Express.js application that limits request JSON body size up to 100kb by using express.json(). express.json() uses bytes package that allows to use units like: b, kb, mb, etc. (also: B, KB, MB, etc.).
const express = require('express'); // npm install express
const app = express();
app.use(
express.json({
limit: '100kb'
})
);
app.post('/path/to/api', (request, response) => {
const body = request.body; // <--- JSON body type in request will be limited up to 100KB
// ...
response.send('Done!');
});
app.listen(8080, () => console.log('Server started on port 8080.'));