Implement a Root-Level Request Logger Middleware - Help Needed

I did find the code in question, actually. But I don’t know how to fix it.

Deploying to Vercel seems complicated to me right now. I’ll look into it more later. For now I guess I’ll skip the challenges that have the bug.

@Sky020 You said to edit the package.json file, but can’t I make a pull request on the GitHub repo, fix the code there, and then push it back in? I think I have an idea on how to fix the issue: write an if statement checking whether the thing being passed in to JSON.parse is valid JSON and only pass it in to JSON.parse if it is. I just need to figure out how to code the if condition to have it do that.

The code is in a try-catch block but the code still crashes with an unhandled exception due to what’s passed to JSON.parse not being valid JSON. I don’t get why it’s still an unhandled exception. But I think I can still write code to check if it’s valid JSON before passing it in and fix it that way.

This is not necessary.

When you clone the boilerplate, you get a line in the package.json like this:

"fcc-express-bground": "https://github.com/freeCodeCamp/fcc-express-bground-pkg.git"

This instructs the library (package) to be installed into a folder named node_modules (again, on Repl, you cannot see the folder, but locally you can). Then, you can navigate to the package within the node_modules folder, and edit it there. This will affect only your app.

This is not specific for freeCodeCamp, and works the same for every package in a node app.

This is all done here: fix: .env challenge and related by ShaunSHamilton · Pull Request #7 · freeCodeCamp/fcc-express-bground-pkg (github.com) (from the above linked issue). As you can see in the Repl, I linked in this (please do not edit it, as this will change my Repl), I manually installed the fcc-bground library (package), and edited it in Repl. This has not changed the boilerplate, but has allowed me to:
a) Test changes to the package
b) Pass the challenges


It is unhandled by the next middleware in the chain:

try {
        lowerCase = JSON.parse(lowerCase);
      } catch (e) {
        console.log(e);
        process.env.MESSAGE_STYLE = msgStyle;
        next(e);
      }

You can see the e (error), is being passed to the next middleware. The next middleware is causing the app to crash with:

_http_outgoing.js:518
    throw new ERR_HTTP_HEADERS_SENT('set');
    ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:518:11)
    at ServerResponse.header (/home/runner/boilerplate-express-2/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/home/runner/boilerplate-express-2/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/home/runner/boilerplate-express-2/node_modules/express/lib/response.js:267:15)
    at /home/runner/boilerplate-express-2/node_modules/fcc-express-bground/index.js:108:15
    at IncomingMessage.<anonymous> (/home/runner/boilerplate-express-2/node_modules/fcc-express-bground/index.js:33:7)
    at IncomingMessage.emit (events.js:315:20)
    at IncomingMessage.Readable.read (_stream_readable.js:505:10)
    at flow (_stream_readable.js:1005:34)
    at resume_ (_stream_readable.js:986:3) {
  code: 'ERR_HTTP_HEADERS_SENT'
}

So, the JSON/(lack there of) is not causing the crash directly, but is leading to this other error.

Hope this clarifies.

Thanks. I finally got it.

Never mind. I managed to solve the .env file challenge, but the Root-level logger one is still giving me some trouble.

Here’s my code:

const express = require("express");
const app = express();
const homePath = `${__dirname}/views/index.html`;
const assetsPath = `${__dirname}/public/`;
require("dotenv").config();

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

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

app.get("/now", (req, res, next) => {
  req.time = new Date().toString();
  next()
}, (req, res) => {
  res.json({time: req.time});
});

app.get("/", (req, res) => res.sendFile(homePath));

app.get("/json", (req, res) => {
  const message = "Hello json";
  const messageUpper = message.toUpperCase();

  if (process.env.MESSAGE_STYLE === "uppercase") {
    res.json({"message": messageUpper});
  } else {
    res.json({"message": message});
  }
});

What did I do wrong? I also tried doing app.use("/", (res, req, next) => { ... } but that also didn’t work.

Here’s my full code. I did what Lasjorg said.

I also tried this:

const express = require("express");
const app = express();
const homePath = `${__dirname}/views/index.html`;
const assetsPath = `${__dirname}/public/`;
require("dotenv").config();

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

app.get("/now", (req, res, next) => {
  req.time = new Date().toString();
  next()
}, (req, res) => {
  res.json({time: req.time});
});

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

app.get("/", (req, res) => res.sendFile(homePath));

app.get("/json", (req, res) => {
  const message = "Hello json";
  const messageUpper = message.toUpperCase();

  if (process.env.MESSAGE_STYLE === "uppercase") {
    res.json({"message": messageUpper});
  } else {
    res.json({"message": message});
  }
});

That also doesn’t work. Is this also due to some sort of bug or is it really my code that’s off this time? Why doesn’t it think that the middleware is active?

I am not sure what errors you are getting, or what lesson you are on, but I will repeat this:

I tried that already.

And no, there are no errors in the code itself and it’s not crashing. It’s only that the test on the challenge doesn’t pass.

And the challenge is the one this thread was opened for: " Implement a Root-Level Request Logger Middleware". I already solved the one before it (the .env one).

Here’s the latest code. The middleware function is mounted before all the routes there but it still doesn’t pass the challenge. Could it be that it logging undefined undefined - undefined is part of the problem? Or is it that the fact that that’s by nature in REPL makes it “okay”?

Here is your problem:

app.use((res, req, next) => {

Look at the order of your parameters…

Hope this helps

Yeah, it helped.

Silly mistake there. Thanks.