Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda