# freeCodeCamp: Basic Node and Express - Use .env file

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-basic-node-and-express-use-env-file/438378>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [January 2, 2021, 8:42am UTC](https://forum.freecodecamp.org/t/freecodecamp-basic-node-and-express-use-env-file/438378 "2021-01-02T08:42:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pl270812](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pl270812/32/196930_2.png) [@pl270812](https://forum.freecodecamp.org/u/pl270812)\
**Post date:** [January 2, 2021, 8:42am UTC](https://forum.freecodecamp.org/t/freecodecamp-basic-node-and-express-use-env-file/438378/1 "2021-01-02T08:42:04Z")

</div>

**Tell us what’s happening:**  
Hi, I’m newbie and start learning Nodejs on freeCodeCamp. I’m at the lesson “[https://www.freecodecamp.org/learn/apis-and-microservices/basic-node-and-express/use-the--env-file](https://www.freecodecamp.org/learn/apis-and-microservices/basic-node-and-express/use-the--env-file)” and get stuck there. Please help me check my code and correct it. Thanks.

**Your code so far**

```auto
require('dotenv').config();
var express = require('express');
var app = express();

console.log("Hello World");
app.get("/json", function(req, res) {
  if (process.env.MESSAGE_STYLE === "uppercase") {
  res.send("{message: " + "Hello Json".toUpperCase() + "}");
  } else {
  res.send("{message: Hello Json");
  }
});
app.use(express.static(__dirname + "/public"));
module.exports = app;

```

\*\* Result message \*\*  
The response of the endpoint /json should change according to the environment variable MESSAGE\_STYLE

**Your browser information:**

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

**Challenge:** Use the .env File

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/back-end-development-and-apis/basic-node-and-express/use-the--env-file)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![JacksonBates](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jacksonbates/32/123402_2.png) [@JacksonBates](https://forum.freecodecamp.org/u/JacksonBates)\
**Post date:** [January 2, 2021, 11:30am UTC](https://forum.freecodecamp.org/t/freecodecamp-basic-node-and-express-use-env-file/438378/2 "2021-01-02T11:30:37Z")

</div>

Have you included a `.env` file in the root of the project?

To be clear, the `.env` file is literally called `.env`, that’s not an extension, it’s the whole name - that catches people out sometimes…

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [January 2, 2021, 1:20pm UTC](https://forum.freecodecamp.org/t/freecodecamp-basic-node-and-express-use-env-file/438378/3 "2021-01-02T13:20:23Z")

</div>

Welcome, there.

I recommend you go over the previous lesson:  
[Basic Node and Express - Serve JSON on a Specific Route | Learn | freeCodeCamp.org](https://www.freecodecamp.org/learn/apis-and-microservices/basic-node-and-express/serve-json-on-a-specific-route)

Specifically:

> Inside the route handler, use the method `res.json()` , passing in an object as an argument. This method closes the request-response loop, returning the data. Behind the scenes, it converts a valid JavaScript object into a string, then sets the appropriate headers to tell your browser that you are serving JSON, and sends the data back. A valid object has the usual structure `{key: data}` . `data` can be a number, a string, a nested object or an array. `data` can also be a variable or the result of a function call, in which case it will be evaluated before being converted into a string.

Hope this helps

---

<div class="post-metadata">

**Author:** ![pl270812](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pl270812/32/196930_2.png) [@pl270812](https://forum.freecodecamp.org/u/pl270812)\
**Post date:** [January 3, 2021, 4:58am UTC](https://forum.freecodecamp.org/t/freecodecamp-basic-node-and-express-use-env-file/438378/4 "2021-01-03T04:58:51Z")

</div>

> [@pl270812](#):
>
> ```auto
> require('dotenv').config();
> var express = require('express');
> var app = express();
> 
> console.log("Hello World");
> app.get("/json", function(req, res) {
> if (process.env.MESSAGE_STYLE === "uppercase") {
> res.send("{message: " + "Hello Json".toUpperCase() + "}");
> } else {
> res.send("{message: Hello Json");
> }
> });
> app.use(express.static(__dirname + "/public"));
> module.exports = app;
> 
> ```

I did. Thanks Jackson.

---

<div class="post-metadata">

**Author:** ![pl270812](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/pl270812/32/196930_2.png) [@pl270812](https://forum.freecodecamp.org/u/pl270812)\
**Post date:** [January 3, 2021, 5:00am UTC](https://forum.freecodecamp.org/t/freecodecamp-basic-node-and-express-use-env-file/438378/5 "2021-01-03T05:00:11Z")

</div>

I did a mistake using the method res.send(). Thanks.

---

<div class="post-metadata">

**Author:** ![algamish](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/algamish/32/135585_2.png) [@algamish](https://forum.freecodecamp.org/u/algamish)\
**Post date:** [February 17, 2021, 1:00am UTC](https://forum.freecodecamp.org/t/freecodecamp-basic-node-and-express-use-env-file/438378/6 "2021-02-17T01:00:08Z")

</div>

Hello,  
Can anyone else help me with this problem? I cant get my code to pass, even though when i go to ‘my-app-url/json’ it displays '{“message”: “HELLO JSON”} as the exercise specifies. I’ve been at this for a few days, and have tried every method i can think of to display ‘{“message”: “HELLO JSON”}’. My .env file is also named just ‘.env’ in the root(under the views tab, where myApp.js is located, correct me if my understanding of the root is incorrect). Does anyone have any ideas I might not be thinking of? Here is my code at the moment:  
require(‘dotenv’).config();  
var express = require(‘express’);  
var app = express();

app.get(’/’, function(req, res) {  
//retrieve path of HTML file.  
absolutePath = \_\_dirname + ‘/views/index.html’ ;  
res.sendFile(absolutePath);  
});

app.use(’/public’, express.static(\_\_dirname + ‘/public’));

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

if (process.env.MESSAGE\_STYLE === “uppercase”) {  
response = “Hello json”.toUpperCase();  
} else {  
response = “Hello json”;  
}  
res.json({“message”: response});

});

[//app.use](https://app.use)(express.static(\_\_dirname + “/public”));  
module.exports = app;

console.log(“Hello World”);

The url is as follows:  
‘[https://repl.it/@AlghieDionisio/boilerplate-express#myApp.js](https://repl.it/@AlghieDionisio/boilerplate-express#myApp.js)’

---

<div class="post-metadata">

**Author:** ![Catalactics](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/catalactics/32/151364_2.png) [@Catalactics](https://forum.freecodecamp.org/u/Catalactics)\
**Post date:** [February 17, 2021, 1:02am UTC](https://forum.freecodecamp.org/t/freecodecamp-basic-node-and-express-use-env-file/438378/7 "2021-02-17T01:02:25Z")

</div>

> [@My code seems to work, but the test doesn't pass](https://forum.freecodecamp.org/t/my-code-seems-to-work-but-the-test-doesnt-pass/446310/2):
>
> Hey @DragonOsman, Unfortunately this is a known problem and is currently being fixed on. Here is the github issue: Hopefully this would be fixed ASAP, but for the current time, you can skip this challenge and go on to the next one. Thank you for the understanding…

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [October 17, 2021, 6:26pm UTC](https://forum.freecodecamp.org/t/freecodecamp-basic-node-and-express-use-env-file/438378/8 "2021-10-17T18:26:42Z")

</div>


