Implement a Root-Level Request Logger Middleware

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

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

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

1 Like

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!

1 Like

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

1 Like

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()

5 Likes

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();});
1 Like

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();

});
1 Like

You put req.iq instead of req.ip

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.

3 Likes

Thanks. I wasted my time looking up how to convert from express.logger to morgan, require morgan, and then use morgan in an app.

Is FreeCodeCamp testing our ability to use Express 3 instead of 4?

var express = require(‘express’);
var app = express();

// --> 7) Mount the Logger middleware here
var myLogger = app.use(function(req,res,next)
{

       console.log('LOGGED' + req.method + " " + req.path + " - " + req.ip)
        next();
  });

Hi,
why do we have to put this middleware specificly at the top I know express execute from top to bottom but why if we put it below the first middleware (for static assets) it doesn’t work.
And why if I put a res.send() method the http file will not be displayed. What if I want to print in the webpage for exemple these informations