Morgan with Express does not log static files

361 views Asked by At

Morgan seems to be logging only HTTP requests created using routes like this:

app.get('/', function (req, res) {
  res.send('hello, world!')
})

I am trying to log requests created using express.static(). How do I do that?

Here's my full code:

const express = require('express');
var morgan = require('morgan');
const cors = require('cors');
const app = express();
const port = 3001;

app.use(cors());
app.use(express.static('public'));
app.use(morgan('tiny'))

app.listen(port, () => {
    console.log(`Listening on port ${port}`)
});
1

There are 1 answers

0
Naresh On

Turns out that order matters. Just had to move morgan before express.static():

app.use(morgan('tiny'))
app.use(express.static('public'));