Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
Logging POST body size using morgan when request is received

I am using 'morgan' library to log my request, time it takes and the size of response.

But, for a POST request is there a way I can log the POST request and its body size when it is received?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

  1. Use custom token formats from Morgan module.
  2. Create a token for body and then just add it to your format.

Here is the working sample code snippet you need to add to your app.js:

morgan.token('body', (req, res) => JSON.stringify(req.body));
app.use(morgan(':method :url :status :response-time ms - :res[content-length] :body - :req[content-length]'));
over 4 years ago · Santiago Trujillo Report

0

After going through morgan documentation for about an hour I found a good solution to the problem.

Instead of using morgan-body, logging can can be done using the morgan library itself at the time of request. Even the length of the POST body can be logged quite easily.

Here is how it can be done:

app.use(morgan({format: 'POST body length in bytes :req[Content-Length]', immediate: true}))

Here the 'format' option defines the format of log and the 'immediate' option writes the log at the time request is received

over 4 years ago · Santiago Trujillo Report

0

You can use morgan-body ,this captures all the request types whether be post or get

import morganBody from 'morgan-body';
import express from 'express';
import bodyParser from 'body-parser';

const app = express();

// must parse body before morganBody as body will be logged
app.use(bodyParser.json());

// hook morganBody to express app
morganBody(app);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!