limit
- Default
'100kb'- What it limits
- Maximum request body size, as bytes or a string such as
'5mb'
Fix an Express 413: raise the express.json limit from its 100kb default, read the entity.too.large error, handle multer limits and check the proxy in front.
In Express a 413 comes from body parsing. express.json() and express.urlencoded() reject bodies over their limit, which defaults to '100kb', with an error whose status is 413 and type is 'entity.too.large'. Raise the limit on the route that needs it, or move uploads to multer, and check any proxy in front.
Four quick questions, then a straight answer.
From your answers, this is quietly costing you traffic and nobody has the hours to chase it. Scaplet finds every affected page, drafts the fixes and tracks how AI answers mention you.
A data platform shipped 46 fixes without a ticket.
This looks handled. Re-check after a redesign, a migration or a big content push; that is when it breaks.
Your site is small and you have the time. Work through the steps on this page, starting with the pages that get the most traffic.
limit and length properties, with status set to 413 and type set to 'entity.too.large'. Express's default error handler sets the response status from err.status, so the client sees 413 unless you handle it.limit'100kb''5mb'parameterLimit (urlencoded)The docs recommend not setting a very high limit: payloads above the default, and "payloads of 5 MB or more" in their example, increase memory use and response times.
Order matters: a parser mounted earlier on a path handles the body first. Mount the high-limit parser on the specific path before the global one, as above, so large payloads are not rejected by the default parser.
app.use((err, req, res, next) => {
if (err.type === 'entity.too.large') {
return res.status(413).json({ error: 'Body too large', limit: err.limit });
}
next(err);
});import express from 'express';
const app = express();
// everything else stays at the 100kb default
app.use('/import', express.json({ limit: '5mb' }));
app.use(express.json());
app.post('/import', (req, res) => res.json({ ok: true }));express.json() does not parse multipart forms. File uploads usually go through multer, which has its own limits. Its fileSize default is Infinity, fieldSize is 1MB, and exceeding a limit raises a MulterError with a code such as LIMIT_FILE_SIZE. That error is not an entity.too.large error, so handle it separately.import multer from 'multer';
const upload = multer({ dest: 'uploads/', limits: { fileSize: 10 * 1024 * 1024 } });
app.post('/avatar', (req, res) => {
upload.single('avatar')(req, res, (err) => {
if (err instanceof multer.MulterError) {
return res.status(413).json({ code: err.code });
}
if (err) return res.status(500).end();
res.json({ ok: true });
});
});| Layer | Where to look |
|---|---|
| nginx | client_max_body_size, default 1m; see 413 in nginx |
| Apache | LimitRequestBody; see 413 in Apache |
| IIS | maxAllowedContentLength; see 413 in IIS |
curl -i -X POST -H 'Content-Type: application/json' --data-binary @big.json http://localhost:3000/import.
A 413 with an HTML body and a Server: nginx header came from the proxy, not Express.
Call the app port without the proxy. If it passes, raise the proxy limit instead of the parser limit.
With the default limit you should see your route's normal status for the first file and 413 for the second. If both fail, something other than body-parser is rejecting the request. If both pass, your limit is higher than you think, so look for another express.json() call earlier in the stack.
Content-Type matches its type option, so a mismatched type leaves req.body undefined rather than returning 413.node -e "console.log(JSON.stringify({d:'x'.repeat(90*1024)}))" > under.json
node -e "console.log(JSON.stringify({d:'x'.repeat(110*1024)}))" > over.json
curl -s -o /dev/null -w '%{http_code}\n' -X POST -H 'Content-Type: application/json' --data-binary @under.json http://localhost:3000/
curl -s -o /dev/null -w '%{http_code}\n' -X POST -H 'Content-Type: application/json' --data-binary @over.json http://localhost:3000/3 questions people ask next, answered from the sources on this page.
express.json() and express.urlencoded() use body-parser, whose limit option defaults to '100kb'. Bodies over it produce a 413 error of type entity.too.large.
Pass a larger limit to the parser, for example express.json({ limit: '5mb' }), ideally only on the route that needs it. The docs recommend using the default whenever possible.
Multipart uploads are parsed by multer, which has separate limits. A proxy in front of Express can also enforce a smaller limit and return its own 413.
Run the free audit, then approve the fixes. Every change can be undone.