Implement a Root-Level Request Logger Middleware

Implement a Root-Level Request Logger Middleware
0

#1
app.all((req,response,next)=>{
  var method=req.method;
  var path=req.path;
  var ip=req.ip;
  console.log(method+' '+path+' - '+ip);
  next();
})

what else should I use to complete this challenge


#2

Same problem here, don’t know what is the passing criteria of this challenge…

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

#3

What does the rest of your code look like? Express uses middleware in order, so that could be an issue.


#4

Pay attention to this:

Hint: Express evaluates functions in the order they appear in the code. This is true for middleware too. If you want it to work for all the routes, it should be mounted before them. that is, place your code before the routes!


#5

Hi, i used app.use instead of app.all and that worked


#6

put this line

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

Just after you define var app = express()


#7

All,

I’m still stuck on this. Can anyone provide a suggestion for me based on the following code?

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

I dropped it in the specified section of the exercise and it didn’t work.


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

// --> 7)  Mount the Logger middleware here
app.use((req, res, next) => {console.log(req.method+" "+req.path+" - "+req.iq);next();});

#8

YOU HAVE TO PUT THIS FUNCTION BEFORE ALL OF YOUR FUNCTIONS BECAUSE EXPRESS EXECUTES FROM TOP TO BOTTOM
This is my code with ES6 standard :

app.use((req, res, next) => {

 let string = `${req.method} ${req.path} - ${req.ip}`
 console.log(string) 
   
  next();

});

#9

You put req.iq instead of req.ip


#10

Ok, here’s my solution:

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

// --> 7)  Mount the Logger middleware here
app.use(function(req, res, next){
  console.log(req.method + " " + req.path + " - " + req.ip);
  next();
});

There’s no need to create additional variables I believe (that depends on you though).

Second, you may need to create this route before all other routes. So add this function right after the var app = express; line.

I hope this helps.