Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

140
Vistas
How can i create a logger middleware in express without any package or library

I've trying to create a custom logger middleware without any package or library It's as simple as saving the endpoint, the method and the status code response.

I have the problem when I try to save the status code, since my response has not yet reached the controller. I was trying to understand how morgan does it, because it is the first middleware I use and when my backend responds, it logs the status code.

Is there a simple way without me having to modify all my backend controllers?

Or rather, how can I access the res.status of a controller from this middleware?

const createLog = (req, res, next) => {
  const { method, url } = req;
  const { statusCode, statusMessage } = res;

  console.log(statusCode, statusMessage); // Both null when reach the middleware
  next();
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Try this here:

const createLog = (req, res, next) => {
  res.on("finish", function() {
    console.log(req.method, decodeURI(req.url), res.statusCode, res.statusMessage);
  });
  next();
};
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda