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

I can’t understand plz somebody help me to explain this.

app.get(’/json’, function(req, res, next){
console.log();
next();
})

The challenge asks you to implement a root-level logger middleware.
Let’s break it down into pieces:

  • root-level logger middleware. :
    Basically it means that you should implement a “function” triggered by incoming requests regardless of the path the request is directed to.

  • root-level logger middleware. :
    Nothing more than what it means literally - is just a “function” whose objective is to ‘log’ something ( output to console / write into file / … some data)

  • root-level logger middleware. :
    A “function” executed ‘during the travel’ of your request to the response.

Now, the challenge instructions explicit how you should build this function:

For every request, it should log in the console a string taking the following format: method path - ip

Break it down again:

  • For every request, it should log in the console a string taking the following format: method path - ip:
    It means that you don’t need to be more specific ( app.post, app.delete, app.get, …), you can just use app.use as suggested in the instructions.

  • For every request, it should log in the console a string taking the following format: method path - ip:
    Well, basically here it’s asking you to use the console.log method

  • For every request, it should log in the console a string taking the following format: method path - ip:
    And here it’s telling you what to log ( the arguments of console.log method).

Now the only problem is to retrieve the information you need to log: this is what the following statement was made for:

You can get the request method (http verb) [method path - ip ], the relative route path[method path - ip], and the caller’s ip [method path - ip] from the request object, using req.method, req.path and req.ip

About your code:

app.get(’/json’, function(req, res, next){
console.log();
next();
})
  • You don’t need app.get ( which would be triggered only when the request uses a GET method - you want to execute the function for every incoming request ( GET, POST, DELETE, …)

  • You shouldn’t use/json : it will execute the function only when the requests are directed to the /json path and you want to process all the requests.

  • Inside the console.log method you should use the arguments indicated above.

Good luck! :slight_smile:

36 Likes

app.use( (req, res, next)=>{
console.log("req.method+ ’ ‘+req.path+’-’+req.ip ");
next();
})

Root level logger middleware should be active

3 Likes

Thanks so much I solved it

2 Likes

Thanks for the breakdown and step by step explanation. Really helped

1 Like

Thanks for the breakdown, very helpful!

2 Likes

Thank you a lot for the detailed explanations

1 Like

how, i kinda have the same code as you and error! any hint on what is the problem exactly

read comments in your myApp and put your code after var statements
image

3 Likes

I think the problem was that I was running the other command along with this assignment if you can just comment out other problem solution you will get your solution

2 Likes

Hey all, not sure why I get the error “Root level logger middleware should be active”. I’m sure this is something silly but cannot pass the test, appreciate any feedback

app.use(function (req, res, next) {
// Do something
console.log(req.method + " " + req.path + " " + req.ip);
// Call the next function in line:
next();
});

1 Like

the solution to this ‘challenge’ should have been really easy but i do not know why fcc is not letting you to pass the challenge.

let along that last challenge. remember you wrote a function to see in the file of .env if a variable is = uppercase? a lot of people still do not pass that given the issue was reported 2 years ago.

bookmarking this.

3 Likes

How solution is come ?
let me know ?
are you passed test cases?

I also got the error “Root level logger middleware should be active”. The reason for the error is that when you use

app.get(‘filePath’, (req, res, next) => {
YOUR CALLBACK
})

it’s like you are hard coding some of the elements so technically your code will not be “active” or in simple terms “flexible” meaning that when you change variables it will not work like when you change GET to POST, however app.use works regardless of the HTTP method. Also we don’t include the file path because we want to be “active” when file paths change between users e.g your file path won’t be the same as mine so if I used “/tech/Coders-World” as my path, chances are, it won’t work on you because I hard coded the path.

Another mistake would be putting your middleware at the center of your code, it should always come before everything else. I don’t know what error will that yield but it won’t wok.

I hope this explains the challenge.

Also below is the code I used to solve the problem, its a long route but it’s also clearer for beginners

var myLogger = (req, res, next) => {
let ClientIp = req.ip
let path = req.path
let method = req.method

console.log(method + " " + path + " - " + ClientIp);
next()
}
app.use(myLogger);

It’s the same as

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

I just broke my functions down and set variables for better understanding of what’s going on.

3 Likes

thank you so much. i solved it

anyone can tell me whats wrong with my code?
also, why doesnt the object literal work?
Normal code doesnt work

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

literal

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

my Repl
thx

2 Likes

Hi @ksu0593!

Since this is an older post that hasn’t been active that much in the past few months I would suggest opening up a new topic and posting your question there.

Thanks!