Basic Node and Express - Implement a Root-Level Request Logger Middleware

Tell us what’s happening:
Everytime I run my code I get an error

Your code so far

let express = require('express');
let app = express();

app.use( function middleware(req, res, next) => {
  console.log(`${req.method} ${req.path} - ${req.ip}`)
  next();
});

console.log("Hello World");

app.get("/", (req, res) => {
  res.sendFile(__dirname + "/views/index.html");
})

app.use(express.static(__dirname + "/public"));

app.get("/json", (req, res) => {
  if (process.env["MESSAGE_STYLE"] === "uppercase"){
     return res.json({"message": "HELLO JSON"})
  } else {
      return res.json({"message": "Hello json"})
  }
})

Your browser information:
boilerplate-express.tffree.repl.co
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: Basic Node and Express - Implement a Root-Level Request Logger Middleware

Link to the challenge:

This is invalid syntax. You’re mixing old-school function declaration with the newer ES6 arrow syntax. Also, you should be mounting this middleware on the ‘/’ route.

Also, though it’s correct in the code you posted above, this line is missing a parenthesis in your repl, which will also throw an error:

app.use(express.static(__dirname + "/public");

When sharing a link to your repl, it’s better to share the link to your code rather than to the live project: https://replit.com/@tffree/boilerplate-express

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.