Basic Node and Express using the .envs

I am very confused at how express works. I don’t see any of the /json files, I don’t understand how the index.html and css page interact with the express code. For the challenge, the code is getting the The response of "/json" does not change according to MESSAGE_STYLE

/** 6) Use the .env file to configure the app */ app.get("/json", function(req, res){ if(process.env.MESSAGE_STYLE == "uppercase") res.json({"message": "HELLO JSON"}); else res.json({"message": "Hello json"}) });

Did you make the change in the .env file ?

yes I am just confused at how everything works. How does the index.html know to use the myapp or is it the myapp that calls on the index.html

I think this question does not make sense in Glitch. The website itself makes this connection, just like Codepen.

Hi I just got your code to work but i had to comment out (Blankout) section 5 in myapp.js eg.

/** 5) serve JSON on a specific route */
//app.get("/json", function(req, res) {
// res.json({“message”: “Hello json”});
//});

as this section all so defines “message”.

you also have to define MESSAGE_STYLE in the .env file

Environment Config

MESSAGE_STYLE=uppercase

hope this helps!

1 Like

This one was abit tricky for me, I hope this helps anyone

 app.get("/json", function(req, res){ 
   let message = {"message": "Hello json"}
        if(process.env.MESSAGE_STYLE == "uppercase") {
          message.message = message.message.toUpperCase()
           res.json(message); }
          else res.json(message) 
         });
2 Likes

Thanks for posting your code. It helped me more than the hint - even though it still doesn’t pass the test! Grrrr…

Here is my code -> that works -> So frustrating when it works and doesn’t pass!

app.get('/json', function(req, res) {
  let response = {"message": "Hello json"};
  if (process.env.MESSAGE_STYLE = 'uppercase') {
    response.message = response.message.toUpperCase()
    }
  res.json(response)
})

edit - Now I know why it didn’t pass -> I forgot an equals sign in my ‘if statement’. I’m so silly. Your code still helped me, just not in the way I originally thought it did :grinning:

are you working on windows
because i work in windows and it doesn’t work for me
.env file for me
i had to install dotenv library to read env file