Languages
[Edit]
EN

Express.js - limit request JSON size

3 points
Created by:
Aaron1
568

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.'));

 

References

  1. express - npm
  2. Express 4.x - API Reference
  3. express.json() - API Reference

Alternative titles

  1. Express.js - limit JSON size in HTTP request
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join