I have a fairly straight forward logging middleware function in an Express JS app:
app.use(function (req, res, next) {
const line = `${req.method} ${req.originalUrl} ${res.statusCode}`
console.log(line)
next()
})
And I have this route:
this.app.use('/404', function (req, res) {
res.sendStatus(404)
})
Which logs the following:
GET /404 200
With other routes it seems to always return 200.
How can I fix this so that it accurately logs status codes without changing routes?
My goal here was something quick to tell me if a route was hit and whether or not it succeeded. I don't want to have to edit all my routes to be compatible with it since I'll eventually replace it with an actual logging solution.
This still doesn't work for all routes but works for more of them:
this.app.use(async function (req, res, next) {
await next()
const line = `${req.method} ${req.originalUrl} ${res.statusCode}`
console.log(line)
})