I’m stuck on the “Use the .env file” one, lesson 6, even though my API response is correct on my localhost:3000/json. I tried various ways of re-saving the response variable, or just doing it within the object itself. Also, how do you post in code block format here?
Are you submitting using the /json path? Because you should just be submitting just the root path.
http://localhost:3000
I edited your post. But I didn’t fix the quotes so if you want to paste in your code again just edit the post and paste it in between the backticks.
When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
For the purpose of this exercise, I believe you need to declare your env variable in method’s local scope:
app.get('/json', function(req, res) {
const mySecret = process.env['MESSAGE_STYLE'];
var response = "Hello json";
// rest of your code
}
Once I made it local, the test passed. I had problems with this one as well. To me, this makes sense as you do not want to pollute the app with a variable that should remain private.
I tried declaring the env variable within the function, but still getting the message that "The response of the endpoint /json should change according to the environment variable MESSAGE_STYLE.
It looks fine to me. You might double check the env variable you are using or the link you are pasting into the form to run the test. You may also try using == instead of ===.
It might just be a bit buggy. Try and refresh the browser tab that has the form for the test, also restart the server.
Can you explain a little more? It seems like the instructions said to have the /json for the endpoint url, but I don’t see anywhere too add the _api/use-env-vars path.
Isn’t my code above evaluating the env var within the app.get() route handler?
Yes, you are asked to create a /json route. And yes you or any user can hit that endpoint and see the JSON response.
The internal test needs the root path to be submitted and not the /json path.
The test takes the root URL it is given and then does a request to *someRoot*/_api/use-env-vars which is an internal test route.
So if you give it localhost:3000 it uses localhost:3000/_api/use-env-vars but if instead you give it the localhost:3000/json path it uses that as the root path and the request becomes localhost:3000/json/_api/use-env-vars which isn’t a valid endpoint.
It doesn’t really matter that you know about the internal part of the tests, I’m just explaining why you have to give it the root URL and not the URL with some path.