Use the .env File (need help)

Tell us what’s happening:

Your project link(s)

solution: https://boilerplate-express.henrychang7.repl.co

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36.

Challenge: Use the .env File

Link to the challenge:

var express = require("express");
var app = express();
console.log("Hello World");

app.get("/", (request, response) => {
  response.sendFile(__dirname + "/views/index.html");
});

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

let message = { message: "Hello json" };

app.get("/json", (request, response) => {
  if(process.env.MESSAGE_STYLE === "uppercase"){
    response.json({"message": "HELLO JSON"})
  }else{
    response.json(message)
  }
});

Hey next time could you add your code surrounded by backticks. You can do so by using the ( </> ) button in the editor it greatly improves readability.

As for your code I believe you’re missing the return for both response.json‘s

Hope this helps.

1 Like

Tell us what’s happening:

Your project link(s)

solution: https://boilerplate-express.henrychang7.repl.co

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36.

Challenge: Use the .env File

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

I have tried everything and I am getting nowhere in this.

I am wondering, does that project have an env file?

I mean to say, did you create an env file and store message style?

also, I was checking your solution link on that challenge. and it said, " The response of the endpoint /json should change according to the environment variable MESSAGE_STYLE".

it seems the env file is missing? that is why I am not sure about it.

1 Like

Hi again!

Did you create the .env file and put in a different folder?

or did you not create it like @texas2010 said?

It is hard to tell in the screenshot but are you using smart quotes?

They don’t look like the normal straight quotes used in programming.

I can see a lot of red underlines in your code.

You can check to see if smart quotes is disabled on your computer. That might be contributing to the problem too.

Oh oops. I didn’t notice that quote. I keep wondering why it shows a red line a lot. Somehow I didn’t catch that. Good catch!

1 Like

I got your code working.

You need to add the .env file in the root directory and add MESSAGE_STYLE=uppercase to that file.

and then you need to disable the smart quotes. You can google how to do that for windows.

Hope that helps!


Second picture, is this how to you add the .env file to the root directory
first picture, now i am getting an error.

Hello there,

In general, screenshots are difficult to work with. However, I can see that you have a typo where response is not defined for your /json route.

If you scroll up in the Replit console, you should see more information in the error which could help.

Hope this helps

var express = require("express");
var app = express();

console.log("Hello World");

This is the code i have. feel free to edit to make it correct

app.get("/", (request, response) => {
  response.sendFile(__dirname + "/views/index.html");
});

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

let message = { message: "Hello json" };

app.get("/json", (req, res) => {
  if (process.env.MESSAGE_STYLE === 'uppercase') {
    response.json({ "message": "HELLO JSON" })
  } else {
    response.json(message)
  }
});

Anyone can schedule me a videochat on this. I really want to pass and having a video discussion will make this better. I am free after 4:00 PM EST Today.

Again, still the same issue. Where is response defined here:

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

I am little confused why you changed this

If you scroll up to earlier posts from yesterday you will see that you defined response.

You need to look at your earlier posts and change it back to what it was when you defined response.

It’s solved. We can close this conversation

1 Like

Awesome!

But we don’t usually close threads because future forum users might read this conversation and get help from it. :grinning:

1 Like

Great. I am glad about you solved your challenge. Good job.