Implement a Root-Level Request Logger Middleware

Is there any reason why this code shouldn’t be working for this challenge?
I am working from a local environment

Challenge:

Local console output from solution attempt:
image

freeCodeCamp test output

// running tests
Root level logger middleware should be active
// tests completed

Code:

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

// serve a string
/*app.get("/", (req, res) =>{
 res.send("Hello Express");
});*/

// build a simpler logger
app.use((req, res, next) => {
 let log = `${req.method} ${req.path} - ${req.ip}`;
 console.log(log);
 next();
});

// serve a filepath, __dirname is a node global variable for the absolute path, in this case index.html
app.get("/", (req, res) => {
 let path = __dirname + '/views/index.html';
 res.sendFile(path);
});

// use middleware (express.static) to add static assets to the site
app.use("/public", express.static(__dirname + "/public"));

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

Did you remove the export or is it just missing in the code you posted?

module.exports = app;

That should be at the bottom of the myApp.js file

1 Like

Ah yes it is still in there, I had just accidentally sent it about 100 lines down and missed it in my code snippet.

So you do have it but your code isn’t passing?

I’m pretty sure your code was passing for me when I tested it.

If you have an old version of the boilerplate or the dependencies it can fail because of how Node does the DNS now. It was fixed in the test dependency but you can try adding this to the server.js file to test it.

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

Okay that’s very interesting, those lines have resolved the problem, and the test now passes. Thank you!

I cloned the repo last night from the link provided in the earlier part of the course, so I’m not sure about it being outdated.

That is odd, I remember the PR that fixed it so I’m not sure why that wouldn’t be working for you.

If you look in the node_modules folder for fcc-express-bground-pkg and then look at the wrapper.js file do you see the same code as below?

https://github.com/freeCodeCamp/fcc-express-bground-pkg/blob/main/wrappers.js#L49

If not, can you try deleting the node_modules folder and installing the dependencies again? Just to test it.


Edit: I just cloned the boilerplate and ran your code again and it is passing for me with Node v18.16.0

If you run node -v in the terminal what do you have?

Sure, it wasn’t in there, and I actually had fcc-express-bground.

After deleting and running npm install again I end up with the same fcc-express-bground

and

  // Check if it solves the simple-middleware-logger challenge.
  if (
    msg.match(
      /(GET|POST|PUT|DELETE|CONNECT|HEAD|OPTIONS|TRACE)\s\/.*\s\-\s.*(\d{1,3}\.){3}\d{1,3}/
    )
  ) {
    globals.userPassedLoggerChallenge = true;
  }
}

Edit: I am using v18.15.0. I will try updating it.

Sorry, you have the correct folder name I was posting the repo name and not the folder name. So fcc-express-bground is the correct folder name.

I was using pnpm and it worked just fine. I tried it again using npm and I got the old code as well. Running npm update fixed it.

Not really sure what is going on with that, I’m on a pretty new system so it can’t be a cache issue (I don’t think anyway).

1 Like

After updating to node v18.16.1 (lts) It’s still giving me that same folder (fcc-express-bground) with the same code as my previous post, and the test doesn’t pass without those dns related lines in server.js

I cloned the repo from here: GitHub - freeCodeCamp/boilerplate-npm: A boilerplate for the freeCodeCamp curriculum. yesterday

I think it might be the lock file. If you delete the package-lock.json file in the boilerplate-express folder and do the install does it fix it?

After running npm update the test now passes without the following lines and fixed the wrappers.js file

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

@Qualifier9983 Can you confirm for me that if you clone the repo and delete the package-lock.json before installing the dependencies it works for you?

I did not need to delete the package-lock.json. After running npm update the project started passing.

I know, I was just looking for confirmation. But it has been confirmed and the PR should fix it.

Oh, my mistake. Glad you got it sorted. Thank you for all your help.