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

Tell us what’s happening:
I have cloned the GitHub - freeCodeCamp/boilerplate-express: A boilerplate for the freeCodeCamp curriculum. repo to my computer and am running the challenge locally. When I attempt to run the test I get the following error: Root level logger middleware should be active. I have tried suggestions in previous solutions such as npm update --save and have modified the code several times with no luck.

Your code so far

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

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

app.use("/public", express.static(__dirname + "/public"));

console.log("Hello World");

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

app.get("/json", (req, res) => {
  let message = "Hello json";
  if (process.env.MESSAGE_STYLE === "uppercase") {
    message = message.toUpperCase();
  }
  res.json({ message: message });
});

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/108.0.0.0 Safari/537.36 Edg/108.0.1462.54

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

Link to the challenge:

may be you have stopped the server when you submit the local host.if not then try the challenges using either replit or glitch.

Code that is passing on Replit is also now failing for me locally.

Not sure what the issue is but newer Node.js versions seem to be causing issues with some of the tests. I had to change the localhost to use 0.0.0.0 in another challenge and in this challenge, if I go back to Node.js v16.18.0 it passes the logger test again (from v18).

Node.js v16.18.0 logs

GET /json - ::ffff:127.0.0.1

Node.js v18.12.1 logs

GET /json - ::1

Which is an IPv6 address

I think this is related to Node.js switching the IP order

Node.js now returns IP addresses in the order they are returned from the name resolver/DNS

https://github.com/nodejs/node/issues/40537
https://github.com/nodejs/node/pull/39987

You can set the dns in server.js which should fix it (does for me).

const dns = require('dns');
dns.setDefaultResultOrder('ipv4first');

https://nodejs.org/api/dns.html#dnssetdefaultresultorderorder

Or using nvm/nvm-windows and switching to Node.js 16 should fix it as well (nvm is just a Node.js version manager).

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