Basic Node and Express - Use the .env File

Tell us what’s happening:
Okay, I think something weird is going on.
I did the previous challenge (the one on static files) and it wasn’t working.
I kept on double checking and triple checking my code but it still didn’t work. I finally decided to leave it for later, I came back to it today and did the EXACT SAME THING and it worked.

Fastforward to today, I’m attempting this challenge and it worked on replit but yet was not passing the test case, I thought maybe it was an issue with replit so I tried it on codesandbox and it also worked on codesandbox but still isn’t passing the test.

I’m now frustrated and will come back to it tomorrow.
If it passes the test case then (like the previous challenge) then I’ll be confident that something is truly wrong.

I’ll probably also try the same challenge locally tomorrow to test it.

Your project link(s)

solution: https://qg8l5i.sse.codesandbox.io
solution: https://boilerplate-express.drvnprgrmr.repl.co (turned to a string cause I was only allowed to have two links :sweat_smile: )

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) 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:

Okay second day and it still doesn’t work. I need help with this please! :sob:

can you give the link to the editor/your code other than the live app link?

This is it boilerplate-express - Node.js Repl - Replit

Instructions state:

Then, in the /json GET route handler you created in the last challenge access process.env.MESSAGE_STYLE

You have the following:

const messageStyle = process.env['MESSAGE_STYLE']

Is this in the /json GET route handler? No it is not.

Thanks, it’s now working.

Although i’m still confused as to why it didn’t work before seeing as both programs (passing and failing ones) essentially do the same thing.

Can you explain why it wasn’t working before?
And by why I mean which test case was failing?

Thanks :slightly_smiling_face:

The tests manipulate the environment variable behind the scenes. When you wrote the line above the GET route handler, that line only gets called one time (when the app is started), so your variable messageStyle will always be the same value regardless of what the environment variable gets changed to behind the scenes.

In normal circumstances, environment variables are static/constant but to validate you are using an environment variable in your app, the test has to be able to change it and test your route with different values for MESSAGE_STYLE.

:thinking: okay that makes more sense.
I was beginning to go nuts…slowly :sweat_smile: