node8-express-template
node8-express-template copied to clipboard
Body parser default ContentLength limit
While it would be nice if all JSON payloads were under 100KB, that won't be the case 100% of the time.
Express 4 uses the body-parser package to decode JSON and Raw payloads which is nice but the default is a bit small for my use case.
The template loads the body-parser package here: https://github.com/openfaas-incubator/node8-express-template/blob/master/template/node8-express/index.js#L12
app.use(bodyParser.json());
The parser accepts an options object, one of those options is limit
which is a string parsed by the bytes package or a number of bytes. Body parser parses the limit option using the following code:
var limit = typeof opts.limit !== 'number'
? bytes.parse(opts.limit || '100kb')
: opts.limit
If you decide to only implement the bytes parser (easy option) it could be a very simple change:
app.use(bodyParser.json({ limit: process.env.BODYPARSER_JSON_LIMIT }));