Implement a Root-Level Request Logger Middleware

Hi to all,
I’m stuck on this question:

  • Root level logger middleware should be active
    What could be wrong with my code?
    myApp.js:
// Load environment variables at the very beginning of your app

require("dotenv").config();

const { json } = require("body-parser");

const express = require("express");

const req = require("express/lib/request");

const app = express();

const path = require("path");

// Step 1

app.get("/", function (req, res) {

res.send("Hello json");

});

// Step 2

app.get("/another-route", function (req, res) {

let filePath = path.join(__dirname, "/views/index.html");

res.sendFile(filePath);

});

// Step 3

let publicPath = path.join(__dirname, "/public");

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

// Step 4

let helloObj = { message: "Hello json" };

app.get("/json-default", function (req, res) {

res.json(helloObj);

});

// Step 5 + 6

app.get("/json", function(req, res){

if(process.env.MESSAGE_STYLE==="uppercase"){

res.json({"message":"HELLO JSON"})

}else{

res.json({"message":"Hello json"})

}

})

// Step 7

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

console.log(`${req.method} + " " + ${req.path} + " - " + ${req.ip}`);

next();

})

module.exports = app;

.env:
MESSAGE_STYLE=uppercase
server.js (untouched):

/******************************************************
 * PLEASE DO NOT EDIT THIS FILE
 * the verification process may break
 * ***************************************************/
 
const bGround = require('fcc-express-bground');
const myApp = require('./myApp');
const express = require('express');
const app = express();

if (!process.env.DISABLE_XORIGIN) {
  app.use((req, res, next) => {
    const allowedOrigins = ['https://narrow-plane.gomix.me', 'https://www.freecodecamp.com'];
    const origin = req.headers.origin || '*';
    if(!process.env.XORIG_RESTRICT || allowedOrigins.indexOf(origin) > -1){
         console.log(origin);
         res.setHeader('Access-Control-Allow-Origin', origin);
         res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    }
    next();
  });
}

const port = process.env.PORT || 3000;
bGround.setupBackgroundApp(app, myApp, __dirname).listen(port, () => {
  bGround.log(`Node is listening on port ${port}...`);
});

/******************************************************
 * PLEASE DO NOT EDIT THIS FILE
 * the verification process may break
 * ***************************************************/

Links:

Let me know if I need anything else, and please respond simply and as soon as you want.

if you are trying to share a snapshot, this is not the link

this is a forum, people will respond when they can, please do not hurry people

Oh, sorry about that. I’m still kind of new here.

if you use both the backticks and concatenation, the string doesn’t become what you want.
Look at what appear in the console, it’s not what is asked

or use the browser console and test this:

const req = {method: "GET", path: "/json", ip: "000"};
console.log(`${req.method} + " " + ${req.path} + " - " + ${req.ip}`);

So should I write it like this?

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

you can use this to test it, change the string passed to console.log

The example given is GET /json - ::ffff:127.0.0.1
so test it in the browser console:

const req = {method: "GET", path: "/json", ip: "::ffff:127.0.0.1"};
console.log(`${req.method} ${req.path} ${req.ip}`);
console.log("GET /json - ::ffff:127.0.0.1");

does it match exactly?

Do I modify the req I already have to do this? Or am I thinking about putting the code in the wrong spot?

no, test in the browser console

Okay, got it. I’ll try it now and say what happens after.

I’m having trouble figuring out how to write it in the browser console. If I write it the way you did, it shows this:

bash: const: command not found
bash: syntax error near unexpected token ``${req.method} ${req.path} ${req.ip}`'
bash: syntax error near unexpected token `"GET /json - ::ffff:127.0.0.1"'

Did I misinterpret something again?

no, that is the terminal not the browser console, you can’t write javascript in the terminal
the browser console is the one that is part of the browser, where you can execute javascript, and see the things logged
check how to open it for your browser on google

or, use a different JavaScript environment

Do you mean this?

the task manager is not the browser console

did you use google?

this is the look for the one in Google Chrome

I can execute JavaScript in it

I know that, I just was hovering over it as I was screenshotting it.
For some reason, the developer tools button is grey, and it won’t let me click on it. Why can’t I?

I don’t know, it’s your computer.

It might be my account. I’m going to try using a Guest account on this, so you might not hear from me for a while.
UPDATE: I just realized that I can’t do that because then I’d have to find all my progress and would a difficult amount of time logging back into both the forum and the website, being that I have an incredibly big personal boundary in the way.

what browser are you using? I can open a different profile without need to closing the current one.

Anyway, you can use any JavaScript enviroment. For example, the freeCodeCamp editor, OR saving the code in a .js file and executing it with node, OR codepen, OR …

I’m using a Google Chromebook.
Where can I find freeCodeCamp’s editor?

anywhere that there is a challenge with the editor

How can I see the editor?