Https://www.freecodecamp.org/learn/apis-and-microservices/basic-node-and-express/use-the--env-file

Tell us what’s happening:

Your project link(s)

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:87.0) Gecko/20100101 Firefox/87.0.

Challenge: Use the .env File

Link to the challenge:

The problem

Then, in the GET /json route handler that you created in the last challenge, transform the response object’s message to uppercase if process.env.MESSAGE_STYLE equals uppercase. The response object should become {"message": "HELLO JSON"}.

Solution

app.get('/json', function (req, res) {

    if (process.env.MESSAGE_STYLE == "uppercase") {
        res.json({ "message": "HELLO JSON" })
    } else {
        res.json({ "message": "Hello json" })
    }

})

This fails everytime.

The output

// running tests
The response of the endpoint /json should change according to the environment variable MESSAGE_STYLE
// tests completed

The response of the endpoint /json should change according to the environment variable MESSAGE_STYLE

I was not really sure of what was going on so I did some googling and I found out that I needed a module, dotenv. I added, require('dotenv').config(). Yet the output is the same. It does not work.

The output in the browser is

{
  "message": "HELLO JSON"
}

on https://boilerplate-express.michaelotu.repl.co/json
Any assistance would be highly appreciated.

The boilerplate has changed recently. It appears you are using an older version of it. I suggest forking the latest version. When I copy and paste your code into the latest version, it passes without any problems.

1 Like

Also, if you are using repl.it and use process.env in your code, it will automatically install dotenv globally, so you do not even need to require it. If you are developing locally, you would need to install it to the project and require it.

1 Like

@RandellDawson Thanks, it worked like magic.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.