Hello all!
Could somebody tell me what’s missing in my code for the use of the .env file?
My response doesn’t seem to change accordingly. Thanks!!!
https://boilerplate-express.flautisimo.repl.co
Your browser information:
User Agent is: Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:86.0) Gecko/20100101 Firefox/86.0.
Challenge: Use the .env File
Link to the challenge:
jenovs
March 13, 2021, 12:23pm
2
What is the content of your .env file?
You have put this:
var MESSAGE_STYLE=uppercase;
but a .env isn’t a JavaScript file, you can’t use JavaScript syntax like var in there. If you remove that it should work.
var MESSAGE_STYLE=uppercase;
So, am I failing at using “var” to set the corresponding variable?
jenovs
March 13, 2021, 12:55pm
5
Yes, .env is a configuration file which consists of key/value pairs separated by = (no spaces).
And it should be named .env
Well, I changed it. I named it .env and I store the variable MESSAGE_STYLE=uppercase.
I also updated my code a little as follows…
var response = {"message": "Hello json"};
app.get("/json", (req, res) => {
if (process.env.MESSAGE_STYLE === "uppercase") {
response.message = response.message.toUpperCase();
} res.json(response);
}
);
And yet, it doesn’t seem to work.
var response = {"message": "Hello json"};
app.get("/json", (req, res) => {
if (process.env.MESSAGE_STYLE === "uppercase") {
response.message = response.message.toUpperCase();
} res.json(response);
}
);
Can you find what is going on with it? Many thanks!
Hey @flautisimo ,
The .env file is an environment file. This is where you would store your secrets that you don’t want others to know. Nodejs have a method to get the .env file using the process.env.
The .env file is indeed not a JavaScript file, but it is still a useful file to store your API keys, that is needed on runtime without exposing it to the public.
The .env file is supposed to be simple syntax:
VARIABLE_NAME=VALUE
jenovs
March 13, 2021, 2:00pm
9
Apparently something is wrong with your .env file, because https://boilerplate-express.flautisimo.repl.co/json doesn’t return the correct result.
ILM
March 13, 2021, 2:00pm
10
flautisimo:
var response = {"message": "Hello json"};
app.get("/json", (req, res) => {
if (process.env.MESSAGE_STYLE === "uppercase") {
response.message = response.message.toUpperCase();
} res.json(response);
}
);
I think you need to put everything, including the declaration of response inside app.get() callback
Thanks @Catalactics , I did store my variable as the FCC challenge dictates: MESSAGE_STYLE=uppercase in the .env file I created, but apparently the process.env is not reading it. I’ve no clue!
ILM
March 13, 2021, 2:05pm
12
flautisimo:
I’ve no clue!
have you tried my last message?
You mean like this?
app.get("/json", (req, res) => {
var response = {"message": "Hello json"};
if (process.env.MESSAGE_STYLE === "uppercase") {
response.message = response.message.toUpperCase();
} res.json(response);
}
);
It didn’t work
ILM
March 13, 2021, 2:09pm
15
can you give the link to your code, please?
I am on replit.com and the console doesn’t show me anything if I add console.log to my code…
Here’s my project…
https://boilerplate-express.flautisimo.repl.co/
ILM
March 13, 2021, 2:13pm
17
please give the link to your code, not the live app link where your code is not visible
ILM
March 13, 2021, 2:21pm
19
I’ve forked your code, created the .env file with the variable, and it passes the challenge
what’s the content of your .env file?
Unbelievable!
my .env file reads as follows…
MESSAGE_STYLE=uppercase;
jenovs
March 13, 2021, 2:25pm
21
MESSAGE_STYLE=uppercase;
^