Basic Node and Express - Use the .env File

Challenge: Basic Node and Express - Use the .env File
Hi sorry to ask. I am struggling here. Even examples I have seen make no sense of this. It will not be capitalised but I have lost the thread of why MESSAGE_STYLES appears tagged on the back of process.env
Link to the challenge:


let MESSAGE_STYLE = "Hello json";
if (MESSAGE_STYLE ==="Hello json".toUpperCase()){
  MESSAGE_STYLE = "HELLO JSON" ;
  }else{
  MESSAGE_STYLE;
  };
app.get("/json", function(req, res){ 
process.env.MESSAGE_STYLE(__dirname +  MESSAGE_STYLE)
}); 

Your browser information:

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

“process.env.MESSAGE_STYLE” is a dot notation to refer to the enviroment variable “MESSAGE_STYLE” which is a property of the object “process.env”.

“process.env” holds all the values of environment variables as properties of the object, thus you need to refer to the variable with the dot notation “process.env.MESSAGE_STYLE”.
This reference to the variable is only for reading the value “uppercase” in your script.

If you want to declare the variable and store the value “uppercase”, do as follows:
if you are using replit you need to use secret tabs to store this variable.
If running project locally, store it in a “.env” file.
Don’t store the variable by declaring let message=“uppercase”, it doesn’t work for environment variables.

you can view the link provided by the hints here for the answer.

1 Like

i am not able to find the problem can you help me here is my code :
const express = require(‘express’);
const app = express();
const path = require(‘path’);
const mySecret = process.env.MESSAGE_STYLE;

app.get(‘/’, (req, res) => {
res.sendFile(path.join(__dirname, “views”, “index.html”));
});

app.use(‘/public’, express.static(path.join(__dirname, ‘public’)));

app.get(“/json”, (req, res) => {
if (mySecret === “uppercase”) {
res.json({ “message”: “HELLO JSON” });
} else {
res.json({ “message”: “hello json” });
}
});

module.exports = app;

@w3nch It would be better if you open a new thread when you have questions.

Anyway, process.env.MESSAGE_STYLE needs to be evaluated inside the route handler for the tests to work.

thanks and i will keep that in my mind from now on :slight_smile: