It Isnt Working! - Implement a Root-Level Request Logger Middleware

It Isnt Working! - Implement a Root-Level Request Logger Middleware
0

#1

I can’t understand why is this not working!
help please

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

FCC says “root-level logger is not working as expected”
link to the challenge:
https://learn.freecodecamp.org/apis-and-microservices/basic-node-and-express/implement-a-root-level-request-logger-middleware


#2

Just out of curiosity, why did you put your req in the console.log using string quotes?


#3

// GET IT EASY THIS WAY
//:relaxed:

app.use(function middleware(req,res,next) {
var string = req.method + ’ ’ + req.path + ’ - ’ + req.ip;
res.send(string);
console.log(string);
next();
});


#4

If you call .send on a request, you’re terminating the request and whatever you may have chained next is getting thrown into oblivion…


#5

It is res.send i.e.

sending on response
after the request is successfully completed.
It also means that if the request is not successful, then the next function will be executed…


#6

// EVEN IF I USE
res.json
// THE RED PANDA SMILES :rofl:

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

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