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

130
Views
Creating logger middleware using morgan

I would like to create a middleware using morgan, i tryed to export the middleware function, then require in app.js. I don't kown why this is not working

Middleware:

    const morgan = require('morgan');
    const rfs = require("rotating-file-stream");

    const rfsStream = rfs.createStream("logs/log.txt", {
        size: '10M', // rotate every 10 MegaBytes written
        interval: '1d', // rotate daily
        compress: 'gzip' // compress rotated files
    })

    function logger(req, res, next){
        morgan('tiny', {
        stream: rfsStream
        })
        next();
    }

    module.exports.logger = logger;

App:

    const express = require('express');
    const app = express();
    const PORT = 4000;
    const { logger } = require('./middlewares/logger');

    app.get('/', logger, (req, res) => {
        res.send('Hello World!')
    });

    app.listen(PORT, () => {
        console.log(`app running on port: ${PORT}`);
    });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Morgan itself is a middleware. That means that you should pass it req, res and next.

HTTP request logger middleware for node.js

So, try to change your Middleware file like this:

const morgan = require('morgan');
const rfs = require("rotating-file-stream");

const rfsStream = rfs.createStream("logs/log.txt", {
    size: '10M', // rotate every 10 MegaBytes written
    interval: '1d', // rotate daily
    compress: 'gzip' // compress rotated files
})

const logger = morgan('tiny', {
  stream: rfsStream
})

module.exports.logger = logger;
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!