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:
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);
}
});
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");
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.
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