Basic Node and Express: use the env.file

Hello, I’m stuck for several hours now at this part of the Basic Node lessons.
Seems like a really simple task (link) but my solution never gets accepted.

I made an .env file with as only content:

MESSAGE_STYLE=uppercase

In myApp.js, code looks like this:

var express = require('express');
var app = express();
const messageStyle=process.env.MESSAGE_STYLE;
console.log(messageStyle);
app.get('/json',function(req,res){
  if(messageStyle ==="uppercase"){
    res.json({
      "message":"HELLO JSON"
    })
  }
  else {res.json({
    "message":"Hello json"
  })}
});

Many other people struggled with this aswell, I looked at other topics first for a solution but neither of those worked.
Is this step broken or is there really anyone who can solve this?

Hello there,

All of you logic must be held within the /json route:

const messageStyle=process.env.MESSAGE_STYLE;
console.log(messageStyle);

Any code outside of the route, will only be computed once. This happens at startup of the server, and never again. So, messageStyle never changes, and the else statement will never run.

Hope this clarifies.

1 Like

I wish it did but I only wrote it like that because I have tried 10 other different ways that didn’t work either.
Combine that with the fact that many topic have already been created asking the same question, all of them without solution.
Also the “get a hint” section of the course doesn’t really say anything,so I’m wondering if it isn’t just broken.

Well, unfortunately, there could be many things which could go wrong.

Fortunately, we are developers…and developers debug. Would you mind doing the following:

  1. Outside of any routes, add this: console.log(process.env.MESSAGE_STYLE);
    – Start the server (in Repl, click RUN)
    – Take a note of what is printed to the console
    • If you see undefined, add dotenv to your package.json, and place this code at the top of your myApp.js file: require('dotenv').config();
    • Repeat the above
  2. Ensure the server is running, on the fCC /learn page, submit your project, and open the browser console (Ctrl + Shift + I)
    – Take a note of the output
  3. Open the app in a full browser window, and navigate to this route:
    /_api/use-env-vars
    – Take a note of:
    • What is printed to the window
    • What appears in this browser window’s console (Ctrl + Shift + I within the app window)

After that, report back here what happened :+1:

2 Likes

It might help as well if you linked to your project code on repl.it or wherever you have the live version at.

Thanks both, I remade the project with the exact same code as I had tried before and now it magically works.
Code looks like this:

app.get(’/json’,function(req,res){
if(process.env.MESSAGE_STYLE ===“uppercase”){
res.json({
“message”:“HELLO JSON”
})
}
else {res.json({
“message”:“Hello json”
})}
});

Literally copied it but only works or gets accepted as a correct solution on one of the two projects.
Not really sure why but I will happily leave it be.

My code is actually the same and it isnt working,
I didnt quite understand how to do what sky020 wrote
any suggestions pls? :smiley:
boilerplate-express-2

Welcome, ksu.

When I open you project, this is what I see:
image

The editor is warning that there are syntax errors. Namely, the quotation marks used are not standard. This is often the case, when coding on a mobile phone, or with predictive text.

Also, the commented out code should not be commented out.

1 Like

yaaaaay it works x)
youre right one of the quotes was neither ’ nor "
about the commented out, I understood from the instruction note here that it needs to be commented out…
tyvm!

Glad you got it working. The idea behind the commenting out is you still need one route pointing to / (home route). That is all.

How did you get the test to pass?