.env Basic Node and Express - Use the .env File file

Hello World, Good Morning!
Im stucked in Basic Node and Express - Use the .env File. Im using Repl.it and it does not have an .env file. So i created one and this is what i put in it

MESSAGE_STYLE=uppercase;

and them in myApp.js

app.get("/json", (req, res) => {
  if (process.env.MESSAGE_STYLE === "uppercase") {
  response = "Hello World".toUpperCase();
} else {
  response = "Hello World";
}
});

im doing it right? or what im doing wrong because i didn’t pass the test ):

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 (’).

1 Like

Hello there,

I suggest you take a look again at what text needs to be sent in the message…

oh thanks, i changed but still not working…
my env file is right? i cant just declare the variable and nothing more?

Not quite. ENV variables should not be ended with a semicolon

1 Like

not working yet, don’t know what to do o-o

Unfortunately, I cannot help you further without seeing your current code.

I suggest you share the link to your project, if you are completing it on Repl.it or Glitch.

There is it

Hello again.

You are not serving (responding) anything with the request. So, I suggest you take a look at the previous lesson, how to serve json:

Hope this helps

This part dont serve json?
app.get("/json", (req, res) => {

Not quite. It is shorthand for telling your app this:

app.route('/json').get((req, res) => {// Do something}
  • Tells your app to do something, when a GET request is made to the endpoint /json

That “something” could be “add the numbers 1 and 2”, or “respond with a json object”.

Now, the response is the thing passed to the second argument of the callback function. Information about the request is the first argument.

So, going back to the lesson:

Let’s create a simple API by creating a route that responds with JSON at the path /json . You can do it as usual, with the app.get() method. Inside the route handler, use the method res.json() , passing in an object as an argument. This method closes the request-response loop, returning the data.

I hope this clarifies.

Hello. Thank you for helping me.

I did try to do wrapping the if statement in res.json (i did also try to wrap all the if in a single res.json) but it didn’t work as well…
I still doing it wrong? (sorry im a rookie)

No worries. It is good you are learning.

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

This is correct, as far as responding to a GET request to ‘/json’. However, you should only have unique endpoints per request type.

What that means is:

  • This is fine:
app.get("/json", (req, res) => {
  res.json({message: "Hello json"});
});
app.post("/json", (req, res) => {// Do something})
  • This is not:
app.get("/json", (req, res) => {
  res.json({message: "Hello json"});
});
app.get("/json", (req, res) => {
  res.json({message: "Something else"});
});

Remember, (req, res) => { } is just like any other function.

So, if I asked you to write me a function which accepted a request and response as arguments, but responded with a different message depending on the value of MESSAGE_STYLE, how would you go about writing that?

I did try to do like the hint from the test:

app.get("/json", (req, res) => {
if (process.env.MESSAGE_STYLE === "uppercase") {
	res.json(response = "Hello World".toUpperCase());
} else {
  res.json(response = "Hello World");
}
});

Yes, and you are close, but here is the instructions:

Serve the object {"message": "Hello json"} as a response, in JSON format, to GET requests to the /json route.

… if process.env.MESSAGE_STYLE equals uppercase . The response object should become {"message": "HELLO JSON"}

Do you see the issue?

maybe this ?

app.get("/json", (req, res) => {
	var response = "Hello Json";
if (process.env.MESSAGE_STYLE === "uppercase") {
	response.toUpperCase();
	res.json({"message": ""+ response});
} else {
  res.json({"message": ""+response});
}
});

Remember, programming is usually very specific:

Serve the object {"message": "Hello json"}

var response = "Hello Json";

There is a typo in yours.

Also, this is not assigning the value to anything, so it does nothing:

response.toUpperCase();

An aside: ""+ response the "" are not necessary.

So i’ve made some changes… but its right now? if yes i think the problem is on the env file

app.get("/json", (req, res) => {
	var response = "Hello Json";
if (process.env.MESSAGE_STYLE === "uppercase") {
	response="hello json".toUpperCase();
	res.json({"message": response});
} else {
  res.json({"message": response});
}
});

There is a problem with this line:

var response = "Hello Json";

It is not exactly as it should be.

how it should be them?

maybe i can eliminate the variable doing this?

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