Stuck on Use the .env File Challenge

Tell us what’s happening:
I’m quite stuck on this challenge and can’t tell if I’m missing a step or if there’s a bug that’s stopping me from passing the tests. Bear with me because I’m new at this, but here’s what I’ve tried so far:

  • The challenge says to store the variable MESSAGE_STYLE=uppercase in the .env file. I’m using the repl.it terminal to write the code and there is no .env file to be found. I tried creating one and added:
    VAR_NAME=MESSAGE_STYLE;
    MESSAGE_STYLE=uppercase;

There aren’t any instructions on how exactly to store the environment variable so my first question is how do I do that.

  • Next, I tried using the following to tell the GET /json route handler to transform the response object’s message to uppercase if process.env.MESSAGE_STYLE equals uppercase:

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

If someone could walk me through this challenge I’d really appreciate it.

Your code so far

Your browser information:

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

Challenge: Use the .env File

Link to the challenge:

Welcome, nbradman.

The boilerplate should not contain a .env file. So, it is expected that you create one.

.env files work as line-deliminated text. So, a ; (semi-colon) at the end is included in the value. To answer your first question, you have done most of that part correctly.

You have written two very different things here. The sentence is correct, the code does not reflect it.

I cannot tell exactly, without seeing your full code, but it looks like you are not responding correctly either. So, just to clarify:

// Setup route attached to app
app.route('/json').get((request, response) => {
  // Set the message you want to send
  const myResponseVariable = ...;
  
  // Respond with the data:
  response.json(myResponseVariable);
});

I hope this clarifies.

@Sky020 thanks so much for that. So here’s what my code looks like now:

var express = require('express');
var app = express();
console.log("Hello World");
app.get("/", function(req, res) {
  res.sendFile(__dirname + "/views/index.html");
});
app.use(express.static(__dirname + "/public"));
app.get("/json", (req, res) => {
  res.json({
    message: "Hello json"
  });
});
// Setup route attached to app
app.route('/json').get((request, response) => {
  // Set the message you want to send
  const myResponseVariable = "Hello json";
  
  // Respond with the data:
  response.json(myResponseVariable);
});
var response = "Hello json";
if (process.env.MESSAGE_STYLE === "uppercase") {
  response = "HELLO JSON";
} else {
  response = "Hello json";
}

Is there any chance you can point out what I’m doing wrong here? I’m still not passing the challenge. Thanks again.

Sorry, I seem to have confused you with my previous code. My previous code was just an example, not what you need to pass this exercise.

  • Make sure you only have 1 of each route (eg. /json)
  • Ensure all of your logic for this 1 challenge is contained within the /json route.

To expand on this lesson as much as possible:

  • You are defining a new route for the app - /json
  • You are writing logic for this route, whereby the response depends on the MESSAGE_STYLE variable contained in the .env file
  • Variables within a .env file are accessed using process.env.VARIABLE_NAME
  • The response should be a JSON message with a message key.

When the endpoint (/json) is hit, the logic held within it will be executed. Any logic outside of the endpoint is only ever executed upon the first call to the file (server.js/myApp.js).

To clarify, for all intents and purposes, there is no difference between app.route('/').get(...) and app.get('/', ...).

Hope this is more clear.

If you are completely lost, I highly recommend resetting the code, and starting again. It would be very beneficial