Basic Node and Express - Use the .env File

Hello, there.
I have a problem passing this test.
here’s the question.

Create a .env file in the root of your project directory, and store the variable MESSAGE_STYLE=uppercase in it.

Then, in the /json GET route handler you created in the last challenge access process.env.MESSAGE_STYLE and transform the response object’s message to uppercase if the variable equals uppercase. The response object should either be {"message": "Hello json"} or {"message": "HELLO JSON"}, depending on the MESSAGE_STYLE value. Note that you must read the value of process.env.MESSAGE_STYLE inside the route handler, not outside of it, due to the way our tests run.

Your project link(s)

solution: boilerplate-express - Replit

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36

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

Link to the challenge:

here’s my code.

let express = require(‘express’);
let app = express();
console.log(“Hello World”);
app.get(“/”, (req, res) => {
res.sendFile(__dirname + “/views/index.html”);
});

// Assets at the /public route
app.use(“/public”, express.static(__dirname + “/public”));

app.get(“/json”, (req, res) => {
res.json({
message: “hello json”
});
const mySecret = process.env[‘MESSAGE_STYLE’]
});

module.exports = app;

So what exactly are you attempting to accoplish with this code? Do you understand what the question is asking for?

It saying that depending on the value in your MESSAGE_STYLE variable, you should provide a different response. If it says uppercase your output should be uppercase, and if not, not. Instead your code just always responds “hello json”, and then reads the variable and does nothing with it?

Do you have a specific question about what you should be doing?

Thanks. I’ve been learning all along, but it seems like, here, I truly don’t understand the code I’m writing.
and I’ve been stuck with this error for almost a day now.
can you help with the right code and explanation, please? with the question provided.

Well, it’s seems to be attempting to instruct you on environment variables. These are secret variables that you don’t want to share on the website, which would include private settings, and usually all your passwords to databases and API’s and stuff.

For instance, if you wanted to connect to your database, and wrote code like:

MyDatabase = database.connect(username='me', password='d0nT-T3l!')

Well, if someone got a peak at your code, well woops, now they know your password, and can connect to your database and steal your data.

So stuff like that you would store in a .env file. In later classes you’ll use the .env variables to connect to databases. This file doesn’t get shared or copied into GIT or anything, and its where you put your secrets.

Well, Replit doesn’t support .env so instead you create what they call a SECRET variable, like the class notes say. Then in that file/variable you put a setting, in this case ‘uppercase’.

Then the purpose of your website is to check that variable, and depending on what it says, send either a lowercase or uppercase response.

Does that make sense?

Simple example of whats going on here:

let fav_color = 'blue';

if (fav_color=='blue') {
   console.log ("My favorite color is BLUE!!!");
} else {
    console.log("My favorite color is RED!!!");
}

But instead of a simple variable, and printing to console, you’re reading an ENV variable, and sending a json response.

It is solved. Thank you!