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

Tell us what’s happening:

I am getting following error when submiited for check.I don’t understand what the error means?

Root level logger middleware should be active.

Your code so far

let express = require('express');
let app = express();
require('dotenv').config();

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

// app.use("/public", express.static(__dirname + "/public"))
app.use(function(req, res, next){
    res.send( console.log(req.method +" "+ req.path +" - "+ req.ip));
})
 module.exports = app;

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

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

Hi @mahassan

Try removing just the res.send() part.

Happy coding

If I remove that, it will removed all the items that is said in the exercise

Keep the contents, just remove the wrapper.

You are asked to console.log the properties, not res.send a console.log (which doesn’t work anyway).

You have to call next() after you log the values.

@mahassan @Teller @lasjorg

Hi.
I faced this issue even with corrected code and didn’t find any workaround. :tired_face:

Finally, I came across a comment.. mentioning to add the following to server.js file.

const dns = require(‘dns’);
dns.setDefaultResultOrder(‘ipv4first’);

This was ironic, because there’s a big “PLEASE DO NOT EDIT THIS FILE” comment on top of server.js. :squinting_face_with_tongue:

Anyway, this worked, and the test passed!
Maybe the boilerplate-express → server.js file should be updated to include this from the get go, to avoid this issue.

The Issue

  • By default, Node.js may return IP addresses in IPv6 format (like ::1 for localhost) instead of IPv4 (127.0.0.1).
  • The freeCodeCamp test suite expects the logger output to show the IP in IPv4 format (e.g., 127.0.0.1 or ::ffff:127.0.0.1), not pure IPv6 (::1).
  • When Node.js returns IPv6 addresses, your logger outputs something like:

GET / - ::1

instead of:

GET / - 127.0.0.1

  • The test fails because it’s looking for the IPv4 format.
1 Like

Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.