Zuko
July 24, 2023, 9:59pm
1
This problem has me a bit confused. Can someone help me understand it?
let express = require('express');
let app = express();
app.get("/", (req, res) => {
res.sendFile(absolutePath = __dirname + '/views/index.html');
});
app.use("/public",express.static(__dirname + '/public'));
app.get("/json", (req, res) => {
const mySecret = process.env['MESSAGE_STYLE'];
if (mySecret === 'uppercase') {
res.json({"message": "HELLO JSON"});
} else {
res.json({"message": "Hello json"});
}
});
//The challenge is the code below
app.post(req.method, res.path, next.ip) {
console.log("GET /json - ::ffff:127.0.0.1");
next();
}
module.exports = app;
I would suggest reading docs. The docs call it an Application-level middleware.
You need to app.use()
the middleware/function.
The middleware has to run before all other routes. An Application-level middleware runs before all other routes simply by its location before the routes. It is for the same reason 404 routes are often just a catch-all at the end. Anything that wasn’t handled before it can by definition be seen as a 404 (not found).
1 Like
Zuko
July 25, 2023, 12:51am
3
Thanks!! Finally got it, it’s embarrassing how long that took me but I got
let express = require('express');
let app = express();
app.use((req, res, next) => {
console.log("GET /json - ::ffff:127.0.0.1")
next()
})
app.get("/", (req, res) => {
res.sendFile(absolutePath = __dirname + '/views/index.html');
});
app.use("/public",express.static(__dirname + '/public'));
app.get("/json", (req, res) => {
const mySecret = process.env['MESSAGE_STYLE'];
if (mySecret === 'uppercase') {
res.json({"message": "HELLO JSON"});
} else {
res.json({"message": "Hello json"});
}
});
module.exports = app;
No, you are hard-coding the values. That is cheating, lol.
The req
object has the method
, path
, and ip
properties on it you can use.
Zuko
July 25, 2023, 1:23am
5
Oops LOL…thought I had it, they let me move on and everything, they should’ve been like
I’m so lost, this section got me feeling dumb, been on this all day. The previous ones weren’t even this bad
let express = require('express');
let app = express();
app.use((req, res, next) => {
console.log("req.method req.path - req.ip")
next()
})
app.get("/", (req, res) => {
res.sendFile(absolutePath = __dirname + '/views/index.html');
});
app.use("/public",express.static(__dirname + '/public'));
app.get("/json", (req, res) => {
const mySecret = process.env['MESSAGE_STYLE'];
if (mySecret === 'uppercase') {
res.json({"message": "HELLO JSON"});
} else {
res.json({"message": "Hello json"});
}
});
module.exports = app;
Zuko
July 25, 2023, 1:35am
6
Got it!! My last one was close I just needed to put it inside {} like I’ve seen passing props in react. Still confused to what exactly is going on though. Hopefully this isn’t cheating too lol:
app.use((req, res, next) => {
console.log(`${req.method} ${req.path} - ${req.ip}`);
next();
});
1 Like