.env file challenge not passing test case with SECRET

Tell us what’s happening:
I’m using repl.it to solve this Challenge,
Here’ the code :


var express = require(‘express’);

var app = express();

if(process.env.MESSAGE_STYLE === “uppercase”){
response = “Hello json”.toUpperCase();
} else{
response = “Hello json”
}

app.get(’/json’, (req, res)=>{
res.json({“message”:response})
})


and I’m creating a secret in the secret tab named
key = MESSAGE_STYLE
value = uppercase

It’s changing the value of response based upon the value of MESSAGE_STYLE in the local Environment of replit as it was asked in the challenge, but the test case is not passing.

Your project link(s)

solution: https://replit.com/@akshaysingh02/boilerplate-express-2

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36.

Challenge: Use the .env File

Link to the challenge:

Welcome there,

You are close, but think about when your code runs…

When do you think the response is set?
What do you think happens, if MESSAGE_STYLE is changed, whilst your app is running?

Hi, @Sky020
Thanks for replying.

Sorry, I didn’t quite get it, can you please elaborate.
When I’m changing the value of MESSAGE_STYLE on the /json route its changing into uppercase and lowercase depending upon the value in the secret tab.

Sure. Let us run through what the code/Node/Express app is doing:

  1. You start your app (typically with a command like node ./server.js)
  2. The code is run from top-to-bottom (once!!! - very important)
  3. For a line like this:
let test = "hello";
app.get('/', (req, res) => {
  res.json(test);
});
// -----------------------------------
// I am going to rewrite it like this:
function myCallback(req, res) {
  res.json(test);
}
app.get('/', myCallback);
  • Now, the app object is defined to have a route handler (myCallback) which is executed (run) every time a GET request is made to the route /. According to your app this is transpiled to:
function myCallback(req, res) {
  res.json("hello"); // This is "set in stone"
}
app.get('/', myCallback);
  • If I change test to equal "HELLO", nothing in my app definition is going to change, because the app was initially defined with "hello", and you are not redefining the whole app - just the variable test.

So, when (where) in the code execution would it make sense to re-define test?

Thanks for the explanation @Sky020

What I understood is by changing the environment variable(secret in repl) won’t affect the previous app definition and if that’s the case how to reload or re-render the app again when the value of the environment variable is edited?

you need to consider where is the appropriate place to check the value of MESSAGE_STYLE, if you do it in the global scope it will be always the same.
If you do it in a function then the value will be that of when the function is called

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