Use of the .env File

Tell us what’s happening:
I have no ideea why this is not working. Tweaked solutions here and there. Created my .env file , added the correct code to it. No, freakin’ ideea. refreshed my browser more times than I can count.

Pls help. I’ve been stuck on this for the past 2 days. smh

Your code so far

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

// --> 7)  Mount the Logger middleware here

// --> 11)  Mount the body-parser middleware  here

/** 1) Meet the node console. */
/*console.log("Hello World");

/** 2) A first working Express Server */
/*function(req, res) {
  res.send('Hello Express');
/** 3) Serve an HTML file */
/*app.get('/', (req, res)=> {
 res.sendFile(__dirname + '/views/index.html');

/** 4) Serve static assets  */
/*app.use(express.static(__dirname + "/public"));*/

/** 5) serve JSON on a specific route */

app.get('/json', function(req, res) {
  let message = "Hello json"
  if (process.env.MESSAGE_STYLE === 'uppercase' ) {
    return message = "HELLO JSON";
    "message": message
/** 6) Use the .env file to configure the app */
let msg =
  process.env.MESSAGE_STYLE === "uppercase" ? "HELLO JSON" : "Hello json";
app.get("/json", (req, res) => res.json({ message: msg }));

Your browser information:

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

Challenge: Use the .env File

Link to the challenge:

Welcome, nelly.

Could you please include the link to your project? It will be easier to debug, with it.

I’ve edited your post for readability. 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 (’). here is the link to my repo .

I’ve searched long and wide throughout the forum but all the issues with this challenge were from glitch. Not .So I am not sure as to why it is not working. Even went as far as copying the solution and modifying it to match my code. But to no success. I’m starting to belive there is an issue with and not with the code.

Thanks, for linking your project.

I managed to pass with your code. I forked your app, and added a .env , but all the rest was your code. Could you ensure your .env file has this exact variable:


Edit: Scratch all of that.

The issue is with your function here:

app.get("/json", (req, res) => { 
  let message = "Hello json"; (process.env.MESSAGE_STYLE) ? message=message.toUpperCase() : message=message; res.json({"message": "HELLO JSON"}); });

You are always returning the same res

So that means I just have to remove the res.json part and it should work ?

Not quite.

You definitely need to return something (always), and that is done with res.json. However, you should be returning something different, based on the value of process.env.MESSAGE_STYLE.

In your code, it always returns:

{"message": "HELLO JSON"}

So, change that.

I’d change it but that’s what I have to return apparently . That’s what the challenge is . Idk , I feel like I’m acting all stupid and blind on this one …

Store the variable MESSAGE_STYLE=uppercase in the .env file. Then tell the GET /json route handler that you created in the last challenge to transform the response object’s message to uppercase if process.env.MESSAGE_STYLE equals uppercase . The response object should become {"message": "HELLO JSON"} .

Correct. So, let us break it down:

  1. res.json should be used to respond with an object
  2. The object should contain a parameter named message
  3. The value of message should change (be dependent on) based on process.env.MESSAGE_STYLE
  4. If process.env.MESSAGE_STYLE is equal to 'uppercase', the value of message should be 'HELLO JSON'
  5. If process.env.MESSAGE_STYLE is not equal to 'uppercase', the value of message should be 'Hello json'

I hope that is more clear.

Loud and clear. Could you please explain why doesn’t want to understand it ?

Your response is not changing:

let message = "Hello json";
(process.env.MESSAGE_STYLE) ? message=message.toUpperCase() : res.json({"message": "HELLO JSON"});

What you have above is:

  • Define a variable named message with the value the message should be if MESSAGE_STYLE is not uppercase
  • This is the equivalent code:
let message = "Hello json";
if (process.env.MESSAGE_STYLE) {
} else {
  res.json({"message": "HELLO JSON"});

Can you see what the issue is?

  • Only if process.env.MESSAGE_STYLE exists, the message variable is changed to be uppercase (you never use the variable)
  • If process.env.MESSAGE_STYLE does not exist, respond with "message": "HELLO JSON"

I suggest you go back to the previous lesson, go through the lesson content, and start from scratch with this step.

Good Ideea @Sky020 . Will do, thank you !
Better off to start with a clean slate on this.

1 Like

I started from scratch with JavaScript . Completed the Basic guide. May I tell you that it’s starting to make a lot more sense now .Thank you for the suggestion!

1 Like