Root-Level Request Logger Middleware

Hi,

Can someone explain why the code below isn’t working?

Thanks

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

Maybe an old node version that doesn’t support arrow functions?

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

this doesen’t work either

1 Like

What error are you getting?
Because this works:

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

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

app.get('/', (req, res) => {
  res.send('ok');
});

app.listen(3000, () => {
  console.log('listening');
});

On the challenge page is written:

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.

Just by looking at the code you’ve given, it’s hard to know if you’ve followed that hint or not.

// running tests

root-level logger is not working as expected

// tests completed

This is the error I keep getting.

Maybe you should just post the whole myApp.js file.

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

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

This is all my code so far. The solution above caused an error after app.listen

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

app.use(function middleware(req, res, next) {
let string = req.method + ’ ’ + req.path + ’ - ’ + req.ip;
console.log(string)
// Call the next function in line:
next();
});

// --> 7) Mount the Logger middleware here

// --> 11) Mount the body-parser middleware here

/** 1) Meet the node console. */

/** 2) A first working Express Server */

/** 3) Serve an HTML file */

/** 4) Serve static assets */

/** 5) serve JSON on a specific route */

/** 6) Use the .env file to configure the app */

/** 7) Root-level Middleware - A logger */
// place it before all the routes !

/** 8) Chaining middleware. A Time server */

/** 9) Get input from client - Route parameters */

/** 10) Get input from client - Query parameters */
// /name?first=&last=

/** 11) Get ready for POST Requests - the body-parser */
// place it before all the routes !

/** 12) Get data form POST */

// This would be part of the basic setup of an Express app
// but to allow FCC to run tests, the server is already active
/** app.listen(process.env.PORT || 3000 ); */

//---------- DO NOT EDIT BELOW THIS LINE --------------------

module.exports = app;

Isn’t your quotes somewhat different from regular quotes?

For some reason when pasted onto here they appear that way, but they’re normal on my console.

Your code passes for me as long as I change the quote marks.

Really weird. I used a different glitch link and now it works! Been banging my head against a wall for 2 hours!

Thanks guys!

From the above code I see required.method and req.path. Those variables should be instantiated before you do req.path and req.method. Or where are you cling those values from, please tell us.

Hi,

Seems to have worked fine without doing that.

I will like to see you working code if you don’t mind. Thanks

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

app.get("/", function(req, res) {
res.sendFile( __dirname + “/views/index.html”);
});

app.use(express.static(__dirname + ‘/public’))

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