Basic Node and Express - Use the .env File

Tell us what’s happening:

Hello everyone,
I was developing code that received an .env file, read its value, compared this value between two possibilities and returned the right one. In my tests on Insomnia this value is returned right, but it fails the freeCodeCamp test.
Does anyone have any idea why? :thinking:

.env file:

{
'MESSAGE_STYLE': 'uppercase'
}

Code:

app.get('/json', function(req, res) {
  const mySecret = process.env['MESSAGE_STYLE']
  let message = 'Hello json';
  let response;
  
  if (mySecret === 'uppercase') {
    response = message.toUpperCase();
  } else {
    response = message;
  };
  
  res.json({response}); 
});

freeCodeCamp test error message:
The response of the endpoint /json should change according to the environment variable MESSAGE_STYLE.

Your project link(s)

Solution: boilerplate-express-1 - Node.js Repl - Replit

Your browser information:

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

Challenge: Basic Node and Express - Use the .env File

Link to the challenge:

I don’t think this is quite the right JSON to return.

The response object should either be {"message": "Hello json"} or {"message": "HELLO JSON"}, depending on the MESSAGE_STYLE value.

The key for this object is response, not message.

1 Like

Thank you, it’s working now!

Solution:

app.get('/json', function(req, res) {
  const mySecret = process.env['MESSAGE_STYLE']
  let message = 'Hello json';
  let response;
  
  if (mySecret === 'uppercase') {
    res.send('{"message": ' + '"' + message.toUpperCase() + '"}');
  } else {
    res.send('{"message": ' + '"' + message + '"}');
  };

});

Good work getting it passing!

I added the spoiler blur to your code since it is a passing solution.

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