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

Tell us what’s happening:
Describe your issue in detail here.
I do not understand the reason this is not working.

app.use("/",function(req,res,next){
  console.log(req.method + " " + req.path + " - " + req.ip);
  next();
});

Your project link(s)

solution: boilerplate-express-2 - Node.js Repl - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.102 Safari/537.36

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

Link to the challenge:

You should be getting an error in the Node console that looks something like:

/home/runner/boilerplate-express-2/myApp.js:32
app.use("/",function(req,res,next){
^

ReferenceError: app is not defined
    at Object.<anonymous> (/home/runner/boilerplate-express-2/myApp.js:32:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/home/runner/boilerplate-express-2/server.js:7:13)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
exit status 1

Hopefully, you understand why you are getting this message.

Hint: You commented out important code at the top that defines what app is.

it is not working yet ;(
https://boilerplate-express-2.aaronargotte.repl.co

require('dotenv').config()
let express = require('express');
let app = express();
let absolutePath=__dirname + "/views/index.html"

app.use(express.static(__dirname + "/public"))
app.get("/",(req,res)=>{
  res.sendFile(absolutePath)
})

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

app.get('/json', (req, res) => {
  process.env.MESSAGE_STYLE === 'uppercase' ? 
    res.json({ "message": "HELLO JSON" }):
    res.json({ "message": "Hello json" })
})


app.use("/",function(req,res,next){
  console.log(req.method + " " + req.path + " - " + req.ip);
  next();
});

Assuming you have added the correct environment variable and value to the replit’s Secrets, then you will still need to modify your code by taking notice of the Note near the bottom of the challenge instructions.

1 Like

I must read the intructions carefully. Thanks for your time and explanation.