# Node and Express Json response Error

**URL:** <https://forum.freecodecamp.org/t/node-and-express-json-response-error/424549>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [October 7, 2020, 5:27pm UTC](https://forum.freecodecamp.org/t/node-and-express-json-response-error/424549 "2020-10-07T17:27:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hasnatali.raja](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hasnatali.raja/32/171138_2.png) [@hasnatali.raja](https://forum.freecodecamp.org/u/hasnatali.raja)\
**Post date:** [October 7, 2020, 5:27pm UTC](https://forum.freecodecamp.org/t/node-and-express-json-response-error/424549/1 "2020-10-07T17:27:25Z")

</div>

**Tell us what’s happening:**  
I am trying to solve the json task where I have to send json response according to an environment variable. I have created an environment variable and imported a dot env file.  
Everything seems to be fine but when I press “I have completed this challenge button” it gives error.

* * *

```auto
// running tests
 The response of the endpoint

```

/json

```auto

should change according to the environment variable

```

MESSAGE\_STYLE

```auto

// tests completed

The response of the endpoint `/json` should change according to the environment variable `MESSAGE_STYLE`

*********************************************************************************

**Your code so far**

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

// --> 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 */
app.get("/a",(req,res)=>{
  res.send("Hello Express")
});

/** 3) Serve an HTML file */
absolutePath = __dirname + "/views/index.html";
app.get("/",(req,res)=>{
  res.sendFile(absolutePath);
});

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

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

app.get("/json1",(req,res)=>{

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

});

/** 6) Use the .env file to configure the app */
 let response = '' ;
 console.log(process.env.MESSAGE_STYLE);
 
 if(process.env.MESSAGE_STYLE==='uppercase'){

  // response = "Hello json".toUpperCase();
  response = "Hello json".toUpperCase();
   
 }
 else{
   response = "Hello json";
 }
 console.log('Bihy',{"style":response})
app.get("/json",(req,res)=>{

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

})
 
/** 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;

```

Here is the REPL repository

“[https://repl.it/@HasnatAli/boilerplate-express-6#myApp.js](https://repl.it/@HasnatAli/boilerplate-express-6#myApp.js)”

**Your browser information:**

User Agent is: `Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0`.

**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:** ![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:** [October 7, 2020, 8:58pm UTC](https://forum.freecodecamp.org/t/node-and-express-json-response-error/424549/2 "2020-10-07T20:58:52Z")

</div>

Welcome, hasnatali.

When do you expect this code to run?

```auto
 let response = '' ;
 console.log(process.env.MESSAGE_STYLE);
 
 if(process.env.MESSAGE_STYLE==='uppercase'){

  // response = "Hello json".toUpperCase();
  response = "Hello json".toUpperCase();
   
 }
 else{
   response = "Hello json";
 }
 console.log('Bihy',{"style":response})

```

* * *

Also, 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.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](http://www.freecodecamp.com/forum/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![hasnatali.raja](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hasnatali.raja/32/171138_2.png) [@hasnatali.raja](https://forum.freecodecamp.org/u/hasnatali.raja)\
**Post date:** [October 7, 2020, 9:06pm UTC](https://forum.freecodecamp.org/t/node-and-express-json-response-error/424549/3 "2020-10-07T21:06:41Z")

</div>

Thanks for cleaning my post, I am new to this.  
When I am running the node server in repl. I am supposed to press challenge complete button on FCC. It is on FCC where I am getting an error.

---

<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:** [October 7, 2020, 9:10pm UTC](https://forum.freecodecamp.org/t/node-and-express-json-response-error/424549/4 "2020-10-07T21:10:07Z")

</div>

Right, but what I am getting at is this:

- When someone/something makes a GET request to `/a`, the following code runs:

```auto
  res.send("Hello Express")

```

That is the only time the code runs.

* * *

Now, going back to this challenge’s instructions:

> Then tell the GET `/json` route handler that you created in the last challenge…

So, when do you want the above mentioned code to run?

---

<div class="post-metadata">

**Author:** ![hasnatali.raja](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hasnatali.raja/32/171138_2.png) [@hasnatali.raja](https://forum.freecodecamp.org/u/hasnatali.raja)\
**Post date:** [October 7, 2020, 9:21pm UTC](https://forum.freecodecamp.org/t/node-and-express-json-response-error/424549/5 "2020-10-07T21:21:42Z")

</div>

1 .This is the code I am supposed to run and I will pass the challenge it if the response is correct.

```auto
/** 6) Use the .env file to configure the app */
 let response = '' ;
 console.log(process.env.MESSAGE_STYLE);
 
 if(process.env.MESSAGE_STYLE==='uppercase'){

  // response = "Hello json".toUpperCase();
  response = "Hello json".toUpperCase();
   
 }
 else{
   response = "Hello json";
 }
 console.log('Bihy',{"style":response})
app.get("/json",(req,res)=>{

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

})

```

In a seperate .env file i created this global variable

```auto
MESSAGE_STYLE=uppercase

```

In the if statement above response variable is assigned “HELLO JSON”  
if MESSAGE\_STYLE has ‘uppercase’ assigned and vice versa.  
up till here, everything is working but when I send it as JSON in as a response to get statement, FCC does not consider it correct.

```auto
res.json({"message": response });

```

---

<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:** [October 7, 2020, 9:31pm UTC](https://forum.freecodecamp.org/t/node-and-express-json-response-error/424549/6 "2020-10-07T21:31:52Z")

</div>

The issue is where you are handling the logic in your code. Currently, `response` is defined on setup (when the `app` is defined). This means, if `MESSAGE_STYLE` changes, `response` will not, because the code only runs once.

The tests check your app is behaving correctly, by changing `MESSAGE_STYLE`. However, the `response` variable does not change, because its logic is set up that way.

Hope this helps

---

<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:** [June 1, 2021, 12:33pm UTC](https://forum.freecodecamp.org/t/node-and-express-json-response-error/424549/7 "2021-06-01T12:33:46Z")

</div>

Closed on June 1, 2021, 12:33pm UTC


