I didn’t find any correct answer for the error I got in my Implement a Root-Level Request Logger Middleware so here is the solution.
const logger = (req, res, next) => {
console.log(`${req.method} ${req.path} - ${req.ip}`);
next();
};
app.get("/", logger, (req, res) => {
res.sendFile('/views/index.html');
});
app.get("/json", logger, (req, res) => {
if (process.env.MESSAGE_STYLE === "uppercase")
res.json({
message: "Hello json".toUpperCase()
});
else
res.json({
message: "Hello json"
});
});