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.
ILM
December 17, 2024, 5:37pm
2
if you are trying to share a snapshot, this is not the link
fdjskalfds:
ASAP
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.
ILM
December 17, 2024, 5:46pm
4
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();
})
ILM
December 17, 2024, 5:51pm
6
ILM:
or use the browser console and test this:
const req = {method: "GET", path: "/json", ip: "000"};
console.log(`${req.method} + " " + ${req.path} + " - " + ${req.ip}`);
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?
ILM
December 17, 2024, 5:58pm
8
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?
ILM
December 17, 2024, 6:08pm
11
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
ILM
December 17, 2024, 6:10pm
13
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?
ILM
December 17, 2024, 6:15pm
15
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.
ILM
December 17, 2024, 6:21pm
17
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?
ILM
December 17, 2024, 6:23pm
19
anywhere that there is a challenge with the editor
How can I see the editor?