Basic Node and Express - Use the .env File

Tell us what’s happening:
I’m struggling to make this work since this is my first time using Replit. I saw the note on the challenge that if we’re using Replit, we have to create the MESSAGE_STYLE=uppercase .env variable on the SECRETS tab. I really don’t know what I should be deleting from the past challenges to make this work.

I think I’ve finally found the SECRETS tab. Is this how I’m supposed to set up and insert the variable into my code?


Screen Shot 2023-01-18 at 10.07.13 AM

Your code so far

let express = require('express');
let app = express();
console.log("Hello World")
const mySecret = process.env['MESSAGE_STYLE']


//app.get("/", function(req, res) {res.send("Hello Express");});
app.get("/", function(req, res) {

  res.sendFile(__dirname + "/views/index.html");
});
app.use("/public", express.static(__dirname + "/public"));

app.get("/json", (req, res) => {
  res.json({if (mySecret === "uppercase") {
  "message": "Hello json".toUpperCase();
} else {
  "message": "Hello json";
}
  });
});

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

Challenge: Basic Node and Express - Use the .env File

Link to the challenge:

Yes the screenshot you showed is the correct one.

You could have skipped the second screen and written your own code for referencing the environment variable.

Hope this helps

You have correctly set up an environment variable named MESSAGE_STYLE with the value uppercase. Now, you just need to make sure you follow the instructions in regards to WHERE to reference the environment variable in the code. You can not just access it anywhere. It must be referenced in a specific place.

It’s good to know that I could’ve skipped that second screen. I updated my code on this post. I’m still getting an error that I’m just throwing things at to fix. Do you know what my problem is?

Thanks for the response. I don’t know if you saw the code that I just added. I thought I was referencing my variable in the right place, but obviously I’m not. Now the compiler doesn’t like the === comparison. What am I doing wrong here?
Screen Shot 2023-01-18 at 10.37.50 AM

The only thing res.json() should have in it is an object. Do not put logic inside it. The logic for creating the object that will be passed to res.json() should go above that line.

I finally got the code below to work. Thank you so much for your help.

app.get("/json", function(req, res){
  const mySecret = process.env['MESSAGE_STYLE'];
if(mySecret == 'uppercase') {
res.json({message: 'HELLO JSON'});
}else {
res.json({message: 'Hello json'});
}
})