Backend and APIs: Basic Node and Express, Lesson 6

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?

app.get(’/json’, function(req, res) {
var response = “Hello json”;
if (process.env.MESSAGE_STYLE === “uppercase”) {
//response = response.toUpperCase();
res.json({“message:”: response.toUpperCase()})
else {
res.json({“message”: response});

Put the code text inside 3 backticks and it will do code formatting. ```Like this```.

Sorry, can’t help with the API portion though.

Are you submitting using the /json path? Because you should just be submitting just the root path.


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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

When I submit using just the root path, I get an error on my VSCODE, but also I don’t pass the test cases:


Also, I have to go to the http:www.localhost:3000/json link to see the “HELLO WORLD”, but then the test cases fail when I submit with that link.

What’s the issue here? The code looks correct

This is the code:


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.

Hope this helps.

@sedstan Thanks for the suggestion.

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.


What am I missing?

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.

That’s all the tips I have.

You have to submit the root, otherwise, it won’t work as the test will hit the wrong endpoint.


and not the one it should hit.


And as said the env has to be evaluated inside the route handler (it’s just because of how the test works).

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?

Sorry if these are noob questions.

  1. Yes, you are asked to create a /json route. And yes you or any user can hit that endpoint and see the JSON response.

  2. 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.

Thanks for explaining. I see, yeah I actually tried that from your previous reply. Submitting the root path actually doesn’t work either:


Which is why I’m really stuck. Any ideas?

Not sure if you’ve fixed it yet, but in your json responses, one says {"message:": response}. I think it has an extra colon.

Wow that was it! Didn’t know because there was no error and it worked at the endpoint. Thanks!!!

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