Tell us what’s happening:
I am in step 6 of Basic Node and Express of Backend and APIs certification. I don’t know why the if statement wouldn’t execute:
app.get("/json",(req, res)=>{
if(process.env.MESSAGE_STYLE == "uppercase"){
res.json({"message":"HELLO JSON"})
}
else{res.json({"message":"Hello json"})}
})
when I attach the /json to my project url the json object of the else statement is displayed.
i have created a .env file with no name and its content is : MESSAGE_STYLE=uppercase
Your code so far
app.get(“/json”,(req, res)=>{
if(process.env.MESSAGE_STYLE == “uppercase”){
res.json({“message”:“HELLO JSON”})
}
else{res.json({“message”:“Hello json”})}
})
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Challenge Information:
Basic Node and Express - Use the .env File
Hi and welcome to the forum.
Are you working locally? (Pls clarify where you are working as that may affect your environment).
no i am not working locally, i am using the gitpod boilerplate
app.get("/json", (req, res) => {
if (process.env.MESSAGE_STYLE && process.env.MESSAGE_STYLE.toLowerCase() === "uppercase") {
res.json({"message": "HELLO JSON"});
} else {
res.json({"message": "Hello json"});
}
});
That doesn’t work either
and yes i did reload(using npm start) the browser after changing the code
what result do you expect
what happens if you just log to the console the value of the process.env.MESSAGE_STYLE? is it what you expect?
ILM
May 18, 2024, 4:02pm
8
have you read that part of the instructions about dotenv?
omg ilenia I literally ignored the instruction seeing its “If you are working locally…” thanks alot wow sh*t
I wanted the json object uppercased or lower according to the value of the the environment variable MESSAGE_STYLE which in this case was “uppercase”.