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

137
Views
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 answers
Answer question

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 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!