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

Why is this not passing?

app.use((req, res, next)=>{
let string = '${req.method} ${req.path} - ${req.ip}';
console.log(string);
  next();
});

I have searched through the forums, on the web, and I still cannot get this to pass. Thanks.

Can you link to your code?
Also, is anything logging? Or is the log empty?

Here’s the code

and the log shows;

Node is listening on port 3000...
*
*
*
https://learn.freecodecamp.org
*

Thanks.

The problem is with this '${req.method} ${req.path} - ${req.ip}'

You are using single quotes (’) instead of backticks (`) while trying to use template strings (using ${}). Change the quotes to backticks (they are located right next to the number 1 on your keyboard).

Easy mistake that I make all of the time :slight_smile: .

Yeah, something simple… fixed it but it still isn’t working. It should be though, shouldn’t it?

// running tests
root-level logger is not working as expected
// tests completed

Darn, I thought that would do it. I’m out of the house now, but I can take another look later when I get home. Hope you figure it out before that. Good luck.

1 Like

So I figured it out and got the test to pass. At the very top of myApp.js, below where they require express, there is a section that says // --> 7) Mount the Logger middleware here.

The middleware needs to be set up BEFORE any of the paths. You had them after you had already set up a few routes. I did the same thing when I was going through these challenges, spent an hour or 2 figuring out that I needed to copy and paste the code a few lines higher. It doesn’t help that the error is very vague. Hopefully this fixes it for you.

1 Like

That’s what did it. Thanks for the second pair of eyes. I didn’t look hard enough I guess. :laughing:

Thanks again @lucassorenson

No problem! When you’ve been looking at the same code for hours, it’s very easy to miss something simple. Most of my problems are either some sort of typo or something silly like this. Glad I could help.

1 Like

Hi ! can u tell me why it still doesnt work ?
/** 7) Root-level Middleware - A logger */
// place it before all the routes !
app.use((req,res,next)=>{
console.log(req.method + " " + req.path + " - " + req.ip);
next();
});