.env challenge couldn't be passed

Tell us what’s happening:
I have implemented the code and it works fine on the Repl.it but when I paste the link for testing it says that
// running tests The response of the endpoint

/json

should change according to the environment variable

MESSAGE_STYLE

// tests completed
my actual output : {“message”:“HELLO JSON”}

also i have created the .env file and have written this inside:
MESSAGE_STYLE=uppercase
Your code so far

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


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

function hello(req,res){
  res.send("Server is running");
}

 module.exports = app;


Your browser information:

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

Challenge: Use the .env File

Link to the challenge:

1 Like

Welcome, aerojex.

Remember, any code outside of a route will only be run once - upon first script initialisation.

This means your logic will never change on changing inputs.

Click for more details:

You need to move your if...else statement logic to be run within the /json route. Otherwise, the value of json_obj will always remain the same.

Hope this clarifies.

I have put the if … else statement inside the json_res function like this

function json_res(req,res){
if(process.env.MESSAGE_STYLE===‘uppercase’){
response=“Hello json”.toUpperCase();
}
else{
response=“Hello json”;
}
json_obj = {“message”: response};
res.json(json_obj);

}

it works fine on the Repl.it server

have a look its active now: https://boilerplate-express.arijitroy3.repl.co/json

but in the challenge window it is not accepting.

YES THE SOLUTION GOT ACCEPTED
clicked the I’ve completed challenge button several times then got accepted.
Might be some server or network issues.
Thankyou @Sky020 for the help

1 Like