Help with the Use the .env File challenge

Honestly, I don’t know antmore

I tried to use the solution that was given by the first hint area and this is the result

I never know why but it always gets rid of the error when I remove the console.log()

With my experience, that the replit show those HTTP traffic error when you opened the port but didn’t close and reopen it again.

This is after the stop and restart, it’s more about the “err”'s in the console that I was talking about

Hello @Hello_Forum,

@caryaharper mentioned this:

Your code:

app.get("/json", (req, res)=>{
  
res.json({
    "message": "Hello json"
  });
  
  var response = "Hello json".toUpperCase();

  if (process.env.VAR_NAME === "uppercase") {
  response = "Hello json".toUpperCase();
} else {
  response = "Hello json";
}
});

Currently, the code does not use the response. You are also not changing it correctly, beacuse you are told to change it based on the MESSAGE_STYLE variable.

This is the expected workflow:

  1. Create a .env file (this file is special, and must be named exactly like that)
  2. Add a MESSAGE_STYLE variable with a value of uppercase in the .env file
  3. Within the existing /json route (from the previous lesson), do the following:
  • If MESSAGE_STYLE is equal to "uppercase", respond with {message: "HELLO JSON"}
  • If MESSAGE_STYLE is not equal to "uppercase", respond with {message: "Hello json"}

For future posts, please include the link to your project code. This helps those wanting to help you.

Hope this clarifies

this is what you send, when are you determining that it is sometimes uppercase? this never changes and is always the same

Okay, I reworked the challenges from the beginning to this challenge and passed them just to make sure I didn’t mess anything up. I also went through the messages that were on this post and reviewed the challenge to se if I missed something and so far I have come up with this:

app.use("/public", express.static(__dirname + "/public"));

app.get("/json", (req, res) => {

var res="Hello json".toUpperCase();
  if(process.env.VAR_NAM===uppercase){
    {message: "HELLO JSON"};
  } else {
    {message: "Hello json"}  }
  return res;
  res.json({
    "message": "Hello json"
  });
});

I also have a file that has only “.env” as it’s name and it is placed as the first file that is under the files named “public” and “views”. As far as I know that is where the “root” of my project directory is. Within the .env file is the code:

MESSAGE_STYLE=uppercase;

As of this point, I have not seen any changes on the display (it still says “Hello HTML.”). Where am I going wrong?

@ieahleen :

I used

because that is what the challenge told me to do

Here is your current code, but with comments:

app.get("/json", (req, res) => {

// There is already a variable name 'res' defined
// DO NOT overwrite it, this will cause problems
var res="Hello json".toUpperCase(); // You do not use this...
  if(process.env.VAR_NAM===uppercase){ // What is 'VAR_NAM'? Where have you defined it?
    {message: "HELLO JSON"}; // This is not doing anything
  } else {
    {message: "Hello json"}  } //  This is not doing anything

  // If a function has a 'return' nothing defined
  // afterwards will do anything. You DO NOT need to
  // return anything in the GET callback - that is why
  // you have the 'res' object.
  return res; 

  // This is almost correct, but the response ('res')
  // is not changing based on the MESSAGE_STYLE value
  res.json({
    "message": "Hello json"
  });
});

Now, the .env file:

  • The .env file is not written in JavaScript
  • It is a plain text file, which is parsed in a special way (LT)
  • anything after the equals sign is included in the value (including a semi-colon)
  • All values are returned as strings
  • So, here is an example of a .env file:
VAR_ONE=value one
VAR_TWO= value two
VAR_THREE="value three";
VAR_FOUR=1234

This is the result of the above:

console.log(process.env.VAR_ONE); // "value one"
console.log(process.env.VAR_TWO); // " value two"
console.log(process.env.VAR_THREE); // '"value three;"'
console.log(process.env.VAR_FOUR); // "1234"

I hope this clarifies

The part of the .env file makes a little more sense. I rewritten the GET and now it looks like this.

ar response="Hello json";
  if(process.env.MESSAGE_STYLE==="uppercase"){
    response="Hello json".toUpperCase();
  } else {
    response="Hello json" ; }
  
  res.json({
    message: "Hello json"
  });

});

I changed the var to response rather than res, possibly corrected the if/else statement, and removed the return statement. I changed the res.json statement back to it’s previous challenges answer. If that is the correct format, I don’t know why the challenge says

The response object should become {"message": "HELLO JSON"} .

I think I’m getting closer, but still needs work, lol :laughing:

This is what the actual response is.

Naming a variable with the word response does not cause it to be what is sent by the API.


Let us go right back to the point of the lessons…

  • You are creating an API, using Express.js, in the form of HTTP requests.
  • The end goal is for someone/something to request some data from your API, by performing a GET request to a specific endpoint (/json)
  • You are defining what happens when someone/something makes a GET request to /json, by saying:
app.get('/json', callback);
  • The app is your app object created by Express.js
  • .get accepts two arguments - a route (/json) - the endpoint - and a callback function (this should be something you are very familiar with by now)
  • The callback function is called with 2 arguemnts(remember, you do not need to call it - this is done automagically behind the scenes)
    • The first argument is the Request object - this holds all the information about the request (e.g. who did it, where they did it from, and what they attached to it)
    • The second argument is the Response object - this is automagically sent to the someone/something which made the request
    • Using this, you, the API developer, need to define what the RESPONSE is (what you want to send as a response)

Now, in this case, you want to send a JSON response with the property message with a value of either "Hello json" OR "HELLO JSON". How do you do this:

function callback(request, response) {
// REMEMBER: you can name the parameters anything you want
// it is just common to name them 'req' and 'res'

// I want to attach a JSON object to the response:
  response.json({property: "value"});
}
// That is it! Express ('app') will automagically reply to the
// requester with the RESPONSE object you have defined

What your code is currently doing:

  • Defining a variable named response with value of `“Hello json”
  • Checking if process.env.MESSAGE_STYLE === "uppercase"
    • If true, setting the variable response to "HELLO JSON"
    • If false, setting the variable response to "Hello json"
  • Attaching a JSON object to the Response object with a property message and a value "Hello json"

What your code should be doing:

  • If process.env.MESSAGE_STYLE === "uppercase", respond with JSON {message: "HELLO JSON"}
  • Otherwise, respond with JSON {message: "Hello json"}

If you are completely lost still:

  1. Go back to the basics, refresh your memory on declarations, objects, functions, and callbacks.
  2. Do some research as to common Express applications - Look at the Express.js Docs
  3. Here is some example code:
app.get('/return-sum-one-plus-one', (req, res) => {
  const a = 1;
  const b = 1;
  res.send(a+b);
  // FYI, I often write "return res.send(a+b);", because it reminds
  // me when my app is returning with the response, but this is
  // not always necessary!
  // REMEMBER: There are many methods for the Response:
  // res.send(), res.get(), res.json(), res.sendFile()....
});

I cannot explain this any more. So, if this has not helped, I recommend you ask a specific question about what you do not understand.

Hope this helps

You are almost there. Only this needs to change:

  res.json({
    message: "Hello json"
  });

Look at the if/else statement. You are setting a variable called response but not using it.