Skip to content

Commit

Permalink
Merge pull request #67 from nigrosimone/cache-body-methods
Browse files Browse the repository at this point in the history
perf: cache body methods on first request
  • Loading branch information
dimdenGD authored Nov 24, 2024
2 parents 068008b + 1374eea commit 141ced3
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,17 @@ function createBodyParser(defaultType, beforeReturn) {
}
if(typeof options.defaultCharset === 'undefined') options.defaultCharset = 'utf-8';

return (req, res, next) => {
const type = req.headers['content-type'];
let additionalMethods;

return (req, res, next) => {

// skip reading body twice
if(req.bodyRead) {
return next();
}

const type = req.headers['content-type'];

req.body = new NullObject();

// skip reading body for non-json content type
Expand Down Expand Up @@ -202,7 +205,7 @@ function createBodyParser(defaultType, beforeReturn) {

// skip reading body for non-POST requests
// this makes it +10k req/sec faster
const additionalMethods = req.app.get('body methods');
if( additionalMethods === undefined ) additionalMethods = req.app.get('body methods') ?? null;
if(
req.method !== 'POST' &&
req.method !== 'PUT' &&
Expand Down

0 comments on commit 141ced3

Please sign in to comment.