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

Tell us what’s happening:

Anyone can take a look at my code? Don’t know what’s the issue here.
app.use((req, res, next) => {
let log = req.method + ’ ’ + req.path + ’ - ’ + req.ip;
console.log(log);
next();
});

Your project link(s)

solution: https://boilerplate-express-2--lareinahu.repl.co

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36

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

Link to the challenge:

The following is the full ocde, currently after I run my code, I didn’t see the login page, only shows Hello Express. Can anyone take a look please? Stuck here for a while now…

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

console.log("Hello World");

app.get("/", function(req, res) {
  res.send('Hello Express');
})

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

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

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

app.get("/json", function(req, res) {
  var jsonResponse = { "message": "Hello json"};
  
  if (process.env.MESSAGE_STYLE === "uppercase") {
   jsonResponse.message = jsonResponse.message.toUpperCase()
  } 
    res.json(jsonResponse);
})


app.get('/json', function(req, res, next){
console.log(`${req.method} ${req.path} - ${req.ip}`);
next();
})

Maybe you should add comments in the first GET route request (where the ‘hello express’ at), in lines 6-8.
This is because the ‘hello express’ that you called is on the same path as the login page, which is called root (“/”).

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