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: