What’s happening:
Here’s my current code in myApp.js:
const express = require("express");
const app = express();
const homePath = `${__dirname}/views/index.html`;
const assetsPath = `${__dirname}/public/`;
require("dotenv").config();
app.get("/", (req, res) => res.sendFile(homePath));
app.use("/public", express.static(assetsPath));
app.get("/json", (req, res) => {
const message = "Hello json";
const messageUpper = message.toUpperCase();
if (req.protocol === "http") {
return res.redirect(`https://${req.headers.host}${req.url}`);
}
console.dir(req.protocol);
if (process.env.MESSAGE_STYLE === "uppercase") {
res.json({"message": messageUpper});
} else {
res.json({"message": message});
}
});
app.use((res, req, next) => {
if (req.protocol === "http") {
return res.redirect(`https://${req.headers.host}${req.url}`);
}
console.dir(req.protocol);
const str = `${req.method} ${req.path} - ${req.ip}`;
console.log(str);
return next();
});
What’s the problem here? The test doesn’t pass. If I need to do an HTTPS redirect here, am I doing it correctly? Would like to ask the same for the challenge before this one since part of the problem for it not passing is because of it not redirecting to HTTPS on its own.
Project link(s)
solution: https://boilerplate-express.dragonosman.repl.co
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68
.
Challenge: Implement a Root-Level Request Logger Middleware
Link to the challenge:
Any help is appreciated. Thanks in advance.