Basic Node and Express - Use the .env File

Cannot pass the test, I have tried a lot of ways and I don’t know what I’m doing wrong.

My code:
let express = require(“express”);
let app = express();
app.get(“/json”, function (req, res) {
var response = process.env.MESSAGE_STYLE;
if (response === response.toUpperCase()) {
res.json({message: response.toUpperCase()});
} else {
res.json({message: response});
}
})

module.exports = app;

https://nebulous-round-dahlia.glitch.me

solution: Glitch :・゚✧

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Basic Node and Express - Use the .env File

Link to the challenge:

if (response === response.toUpperCase())

What does this mean? Your conditional statement should be checking the value of your environment variable.

Read one line above, I did this:
var response = process.env.MESSAGE_STYLE;
I stored the env variable in ‘response’

He means, why are you making the response itself uppercase?

Ok, so what your code does is assign the value of your environment variable (i.e. ‘uppercase’) to the variable response.
Then your conditional statement effectively reads:

if (response === 'UPPERCASE')

Then you will return the following object, in either case:

{message: 'UPPERCASE'}

for i.e. ‘uppercase’ it should go to the else condition, and return it without modification as ‘uppercase’, I thought…

this way it should go to else condition and return ‘uppercase’…
I also tried this:
let express = require(“express”);
let app = express();
app.get(“/json”, function (req, res) {
if (process.env.MESSAGE_STYLE === “allCaps”) {
res.json({message: process.env.MESSAGE_STYLE.toUpperCase()});
} else if (process.env.MESSAGE_STYLE !== “allCaps”) {
res.json({message: process.env.MESSAGE_STYLE});
}
})

module.exports = app;

Your response object should be either:

{message: "Hello json"}

or

{message: "HELLO JSON"}

Where is this in your code?

in the if/else statement, one returns it using toUpperCase() function and the other one returns the original value

All your code is doing is returning the value of your environment variable.
The text ‘Hello json’ is nowhere to be seen. That’s what you should be returning in your response object. You make it uppercase or not depending on the value of your environment variable.

How can I solve it then

nervermind, I solved it, thank you

Ok, I’d start by assigning the text ‘Hello json’ to something.
Then I’d use a conditional statement to check the value of MESSAGE_STYLE.
Then, depending on the outcome of the conditional statement, return a response object containing the text, in uppercase if necessary.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.