.env files not working

Tell us what’s happening:

OK, so I created a .env file with MESSAGE_STYLE=uppercase. Still, the grader isn’t accepting it.

Your code so far


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

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

console.log("Hello world");
// --> 11)  Mount the body-parser middleware  here
app.get("/", (req, res) => {
  res.sendFile(__dirname+"/views/index.html");
});

/** 1) Meet the node console. */

app.use(express.static(__dirname + "/public"));
/** 2) A first working Express Server */
app.get("/json", (req, res) => {
  const message = process.env.MESSAGE_STYLE === "uppercase"?'HELLO JSON':'Hello Json';
  res.json({message});
});

/** 3) Serve an HTML file */


/** 4) Serve static assets  */


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


/** 6) Use the .env file to configure the app */
 
 
/** 7) Root-level Middleware - A logger */
//  place it before all the routes !


/** 8) Chaining middleware. A Time server */


/** 9)  Get input from client - Route parameters */


/** 10) Get input from client - Query parameters */
// /name?first=<firstname>&last=<lastname>

  
/** 11) Get ready for POST Requests - the `body-parser` */
// place it before all the routes !


/** 12) Get data form POST  */



// This would be part of the basic setup of an Express app
// but to allow FCC to run tests, the server is already active
/** app.listen(process.env.PORT || 3000 ); */

//---------- DO NOT EDIT BELOW THIS LINE --------------------

 module.exports = app;

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15.

Challenge: Use the .env File

Link to the challenge:

Welcome, there,

Here is more info on the .env file: https://www.freecodecamp.org/news/nodejs-custom-env-files-in-your-apps-fa7b3e67abe1/

You need to create a file named .env, and add a variable to it called MESSAGE_STYLE, and give that variable a value of uppercase.

Hope this helps

1 Like

@akshit0201 You have to use a package to use custom .env files (Ex. dotenv or custom-env) which I don’t see in your code.

1 Like

I’ve edited my question above. As you suggested I created a .env file and assigned the MESSAGE_STYLE variable a value of uppercase. Even though the code is working and I’m able to see the result as “HELLO JSON” ,the grader isn’t accepting my solution.
Any thoughts?

I don’t know the project structure completely so can’t say properly.

But there is no instance of app.listen()

Would you mind providing the link to your project?

From what I can see, your project structure is quite different compared to mine. For example, my code for this lesson was in step 6.

Right, I do not know why i missed this, but here is the instruction:

The response object should become {"message": "HELLO JSON"}

And here is what you have:

res.json({message});

I am still confused as to why you are on this exercise, when it appears you are missing the code for the previous exercises. As I mentioned before, this should come under step 6.

Hope this helps

Yes, I deleted some code pertaining to previous exercises but I don’t think that should make any difference because my JSON object is changing with the MESSAGE_STYLE variable and that’s what we are supposed to do in this exercise.

I also changed

res.json({message});

to

res.json({"message": message})

which prints {“message”: “HELLO JSON”}

but that doesn’t seem to work.

Thanks for you time :smile: