# Основи Node та Express - Використання .env-файлу

**URL:** <https://forum.freecodecamp.org/t/node-express-env/569655>\
**Category:** General\
**Tags:** українська\
**Created:** [November 7, 2022, 10:01pm UTC](https://forum.freecodecamp.org/t/node-express-env/569655 "2022-11-07T22:01:09Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShevchenkoOl](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shevchenkool/32/289275_2.png) [@ShevchenkoOl](https://forum.freecodecamp.org/u/ShevchenkoOl)\
**Post date:** [November 7, 2022, 10:01pm UTC](https://forum.freecodecamp.org/t/node-express-env/569655/1 "2022-11-07T22:01:09Z")

</div>

**Розкажіть нам, що сталося:**  
Доброго дня, шановні колеги! Поясніть будь ласка послідовність виконання завдання.

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/d/8/9/d893976c94aca4ed240ba7088a3e8030a9e95976.png)  
Тобто, перше я працюю з використанням VS code, в корні я створив файл .env і записав туди зміну var MESSAGE\_STYLE = “Hello World”.toUpperCase();  
 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/6/d/3/6d341d4df7a862c47dbe16ec686100f452081af7.png)  
Після чого в myApp.js я вписую тільки умову виконання завдання if (process.env.MESSAGE\_STYLE === “allCaps”) {  
response = “Hello World”.toUpperCase();  
} else {  
response = “Hello World”;  
}  
або я її вписую в функцію? app.get(“/json”, function (req, res){  
res.json({“message”: “Hello json”})

if (process.env.MESSAGE\_STYLE === “allCaps”) {  
response = “Hello World”.toUpperCase();  
} else {  
response = “Hello World”;  
}});  
Що не так я роблю, дякую!  
**Посилання на ваші проєкти**  
solution: [http://localhost:3000](http://localhost:3000)

**Інформація про ваш браузер:**

Агент користувача: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36`

**Завдання:** Основи Node та Express - Використання .env-файлу

**Посилання на завдання:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/ukrainian/learn/back-end-development-and-apis/basic-node-and-express/use-the--env-file)**
>
> Навчіться кодувати БЕЗКОШТОВНО

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 7, 2022, 10:10pm UTC](https://forum.freecodecamp.org/t/node-express-env/569655/2 "2022-11-07T22:10:44Z")

</div>

> [@ShevchenkoOl](#):
>
> process.env.MESSAGE\_STYLE === “allCaps”) {  
> response = “Hello World”.toUpperCase();  
> } else {  
> response = “Hello World”;  
> }

This logic needs to be inside the callback for your GET. Out here it cannot respond to changes in the environment variable.

---

<div class="post-metadata">

**Author:** ![ShevchenkoOl](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shevchenkool/32/289275_2.png) [@ShevchenkoOl](https://forum.freecodecamp.org/u/ShevchenkoOl)\
**Post date:** [November 7, 2022, 10:18pm UTC](https://forum.freecodecamp.org/t/node-express-env/569655/3 "2022-11-07T22:18:56Z")

</div>

![image](https://global.discourse-cdn.com/freecodecamp/original/4X/9/5/b/95be093a109296e6349e7db1b0fb95ec55c437d5.png)  
Sorry, but nothing, not work!

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 7, 2022, 10:24pm UTC](https://forum.freecodecamp.org/t/node-express-env/569655/4 "2022-11-07T22:24:29Z")

</div>

I said that the logic needs to go inside of the GET; why did you comment out the GET? I’m not sure I understand?

Please post actual code instead of a picture.

---

<div class="post-metadata">

**Author:** ![ShevchenkoOl](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shevchenkool/32/289275_2.png) [@ShevchenkoOl](https://forum.freecodecamp.org/u/ShevchenkoOl)\
**Post date:** [November 8, 2022, 8:17am UTC](https://forum.freecodecamp.org/t/node-express-env/569655/5 "2022-11-08T08:17:50Z")

</div>

Yes, I understud, but I’m not sure if I did the whole sequence correctly. And yet, or this import is correctly specified require(‘dotenv’).config(); and what is it for?

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/8/d/0/8d0337b45490193a6377efe0a8465d2fbdec860e.png)  
not work too

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 8, 2022, 5:12pm UTC](https://forum.freecodecamp.org/t/node-express-env/569655/7 "2022-11-08T17:12:20Z")

</div>

> [@JeremyLT](#):
>
> Please post actual code instead of a picture.

Please post actual code instead of a picture.

* * *

The instructions also don’t mention the environment variable value of `"allCaps"`. You need to read and follow the instructions on what string to return and what environment variable value to check for. If you don’t use the values the instructions tell you to use, then it is impossible for you to pass this challenge.

---

<div class="post-metadata">

**Author:** ![ShevchenkoOl](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shevchenkool/32/289275_2.png) [@ShevchenkoOl](https://forum.freecodecamp.org/u/ShevchenkoOl)\
**Post date:** [November 8, 2022, 11:31pm UTC](https://forum.freecodecamp.org/t/node-express-env/569655/8 "2022-11-08T23:31:38Z")

</div>

```auto
require('dotenv').config();

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

// app.get("/", function(req, res) {
// res.sendFile(__dirname + "/views/index.html");
// });

// app.use("/public", express.static(__dirname + "/public"));

  app.get("/json", function (req, res){
  // res.json({"message": "Hello json"})  

  if (process.env.MESSAGE_STYLE === "allCaps") {
        response = "Hello World".toUpperCase();
      } else {
        response = "Hello World";
 }});

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 8, 2022, 11:34pm UTC](https://forum.freecodecamp.org/t/node-express-env/569655/9 "2022-11-08T23:34:07Z")

</div>

> [@ShevchenkoOl](#):
>
> `"allCaps"`

This is still not the environment variable from the instructions.

> [@ShevchenkoOl](#):
>
> `"Hello World"`

This is still not the message from the instructions.

---

<div class="post-metadata">

**Author:** ![ShevchenkoOl](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shevchenkool/32/289275_2.png) [@ShevchenkoOl](https://forum.freecodecamp.org/u/ShevchenkoOl)\
**Post date:** [November 9, 2022, 7:38am UTC](https://forum.freecodecamp.org/t/node-express-env/569655/10 "2022-11-09T07:38:20Z")

</div>

I didn’t quite understand, that is “allCaps” , need to add instead “Hello World” (like is in the file .env), and instead of response = “Hello World” - response = “hello world”?

require(‘dotenv’).config();

let express = require(‘express’);  
let app = express();

// app.get(“/”, function(req, res) {  
// res.sendFile(\_\_dirname + “/views/index.html”);  
// });

// app.use(“/public”, express.static(\_\_dirname + “/public”));

app.get(“/json”, function (req, res){  
// res.json({“message”: “Hello json”})

if (process.env.MESSAGE\_STYLE === “Hello World”) {  
response = “Hello World”.toUpperCase();  
} else {  
response = “hello world”;  
}});

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 9, 2022, 8:06am UTC](https://forum.freecodecamp.org/t/node-express-env/569655/11 "2022-11-09T08:06:08Z")

</div>

You need to read the instructions

> Створіть файл `.env` в основі каталогу вашого проєкту та збережіть в ньому змінну `MESSAGE_STYLE=uppercase`.

> Then, in the `/json` GET route handler you created in the last challenge access `process.env.MESSAGE_STYLE` and transform the response object’s `message` to uppercase if the variable equals `uppercase`. Об’єкт відповіді має бути `{"message": "Hello json"}` або `{"message": "HELLO JSON"}`, в залежності від значення `MESSAGE_STYLE`.

---

<div class="post-metadata">

**Author:** ![ShevchenkoOl](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shevchenkool/32/289275_2.png) [@ShevchenkoOl](https://forum.freecodecamp.org/u/ShevchenkoOl)\
**Post date:** [November 9, 2022, 9:04am UTC](https://forum.freecodecamp.org/t/node-express-env/569655/12 "2022-11-09T09:04:13Z")

</div>

Sorry, maybe I am stupid, but not understand nothing.  
So, in the file .env , I have variable var MESSAGE\_STYLE = uppercase;  
Then in the /json GET route handler, I access process.env.MESSAGE\_STYLE : app.get(“/json”, function (req, res){ };  
Inside which we write the condition: if (process.env.VAR\_NAME === ‘uppercase’) {  
let response = “Hello World”.toUpperCase();  
res.json({“message”: response})  
}  
else {  
res.json({“message”: “Hello World”});  
}  
})

So the whole code looks like this:  
let express = require(‘express’);  
let app = express();

app.get(“/json”, function (req, res){  
if (process.env.VAR\_NAME === ‘uppercase’) {  
let response = “Hello World”.toUpperCase();  
res.json({“message”: response})  
}  
else {  
res.json({“message”: “Hello World”});  
}  
})

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/a/6/d/a6d8cbdd2eeea7cd48e27565cdd50d0e48b3e695.png)

---

<div class="post-metadata">

**Author:** ![rustamdocstranslator](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rustamdocstranslator/32/286176_2.png) [@rustamdocstranslator](https://forum.freecodecamp.org/u/rustamdocstranslator)\
**Post date:** [November 9, 2022, 10:42am UTC](https://forum.freecodecamp.org/t/node-express-env/569655/13 "2022-11-09T10:42:00Z")

</div>

do not confuse Linux with Javascript.  
In Shell files, which `.env` is you should use such such shell syntax.

```bash
ENVIRONMENT_VARIABLE=whatever

# this is an error cause it is javascript!!!
#var MESSAGE_STYLE = uppercase;

```

also you need to trace your code.  
Add such a code to your js, to see if you see the value of the shell variable.

```javascript
console.log("MESSAGE_STYLE:"+process.env.MESSAGE_STYLE);

```

you need to see in console that it is `MESSAGE_STYLE:uppercase` or the value you assigned.

AND last but the most important use markdown to write code.  
This is how to write code.  
[example article](https://www.freecodecamp.org/news/how-to-format-code-in-markdown/)

````markdown
    ```javascript
    var code = "value"
    ```
    ```bash
    A_SHELL_VAR=value
    ```
    ```java
    String str = "value"
    ```

````

p.s(in ukrainian for clarification):  
не плутайте Linux з Javascript.  
У файлах оболонки, якими є `.env`, ви повинні використовувати синтаксис оболонки shell.

також вам потрібно трасувати свій код.  
Додайте такий код до свого js, щоб побачити, чи бачите ви значення змінної оболонки.

І останнє, але найважливіше, використовуйте markdown для написання коду.  
Ось як пиcати код в markdown.  
3 зворотні галочки.

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [November 9, 2022, 2:02pm UTC](https://forum.freecodecamp.org/t/node-express-env/569655/14 "2022-11-09T14:02:28Z")

</div>

> [@ShevchenkoOl](#):
>
> “Hello World”.

This is still the wrong string.

> [@JeremyLT](#):
>
> `{"message": "Hello json"}` або `{"message": "HELLO JSON"}`,

> [@ShevchenkoOl](#):
>
> process.env.VAR\_NAME

This isn’t correct now.

---

<div class="post-metadata">

**Author:** ![ShevchenkoOl](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/shevchenkool/32/289275_2.png) [@ShevchenkoOl](https://forum.freecodecamp.org/u/ShevchenkoOl)\
**Post date:** [November 10, 2022, 5:50pm UTC](https://forum.freecodecamp.org/t/node-express-env/569655/15 "2022-11-10T17:50:35Z")

</div>

Thank you for helping, everybody! If somebody interesting, I am sending screen with correct code. And variability in the .env file.

 ![image](https://global.discourse-cdn.com/freecodecamp/original/4X/c/4/8/c481dd956ceece65f15e2cff1b9791728815a6ff.png)

---

<div class="post-metadata">

**Author:** ![vrangel08](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/vrangel08/32/383512_2.png) [@vrangel08](https://forum.freecodecamp.org/u/vrangel08)\
**Post date:** [August 8, 2023, 1:07pm UTC](https://forum.freecodecamp.org/t/node-express-env/569655/17 "2023-08-08T13:07:09Z")

</div>

Excellent, but you shouldn`t create ‘response’ variable

---

<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:** [January 7, 2025, 11:47am UTC](https://forum.freecodecamp.org/t/node-express-env/569655/18 "2025-01-07T11:47:24Z")

</div>

Closed on January 7, 2025, 11:47am UTC


