Skip to content

Commit 465644c

Browse files
burtonralexellis
authored andcommitted
Add env var to use raw body and set max json size
This change adds the use of 2 environment variables allowing users to receive the request body as a buffer (the 'raw' body) as well as the ability to set the maximum JSON body size. The raw body is required when processing Stripe webhooks The max JSON body size is defaulted to a low limit from body-parser which has caused issues with users in the past. Signed-off-by: Burton Rheutan <[email protected]>
1 parent 5448ca0 commit 465644c

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

template/node12/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ const app = express()
88
const handler = require('./function/handler');
99
const bodyParser = require('body-parser')
1010

11-
// app.use(bodyParser.urlencoded({ extended: false }));
12-
app.use(bodyParser.json());
13-
app.use(bodyParser.raw());
14-
app.use(bodyParser.text({ type : "text/*" }));
11+
if (process.env.RAW_BODY === 'true') {
12+
app.use(bodyParser.raw({ type: '*/*' }))
13+
} else {
14+
var jsonLimit = process.env.MAX_JSON_SIZE || '100kb' //body-parser default
15+
app.use(bodyParser.json({ limit: jsonLimit}));
16+
app.use(bodyParser.raw()); // "Content-Type: application/octet-stream"
17+
app.use(bodyParser.text({ type : "text/*" }));
18+
}
19+
1520
app.disable('x-powered-by');
1621

1722
class FunctionEvent {

0 commit comments

Comments
 (0)