Problem with passing Basic Node and Express - Use the .env File

Problem with passing Basic Node and Express - Use the .env File
0
#21

Sorry, I didn’t fully understand your answer… code is not formatted.
If I’m not wrong, routes are handled in the order you wrote them. Here’s what I mean:

/** 5) serve JSON on a specific route */
/*
app.get('/json', function (req, res) {
  res.json({"message": "Hello json"});
})
*/

/** 6) Use the .env file to configure the app */
app.get('/json', function(req, res) {
  if (process.env.MESSAGE_STYLE === "uppercase")
    res.json({"message": "HELLO JSON"})
  else
    res.json({"message": "Hello json"})
})
2 Likes

#22

This is nonsenses, the same output and now pass.
and I think is wrong demanded solution, because using patch is better.
The guide is absolut wrong. Maybee is Scottish angliesh

0 Likes

#23

Hi, let me contribute to this thread and share my solution.

  1. Make sure to comment out all previous work done in app.js

  2. We are asked to add MESSAGE_STYLE env variable to .env file like so: MESSAGE_STYLE="uppercase"

  3. Create a new get request and add all the logic there:

app.get('/json', (req, res) => {
  let data = {"message": "Hello json"};
  if(process.env.MESSAGE_STYLE === 'uppercase'){
    data.message = data.message.toUpperCase();
  }
  res.json(data);
});

This way we declare the json object once (let data) and convert the res.json output according to the MESSAGE_STYLE value.

Hope this solution helps you guys.

3 Likes

#24

I found this solution on freecodecamp 14 days ago, but did not work.

I think it the advice there two listening
router at same /json , the res.json could be only one and some combination with others later exercise collide, but that I found in the finals projects.
var message ={“message”: “Hello json”};

app.get('/json', function (req, res ) {
  if (process.env.MESSAGE_STYLE == "uppercase"){
 message.message= message.message.toUpperCase() ;

}else{

  message.message= message.message.toLowerCase() ;

    }
});/*
0 Likes

#25

In this solution ELSE statement is unnecessary. It changes the original message to lowercase which is not asked by the instructions. Also, I can’t find send.json(); logic in the GET root logic provided. So, if you remove ELSE block and add send.json(message) instead it will copy my solution provided above. send.json() is called only once and takes json object message which is changed (or unchanged) according to previous logic in the block.

0 Likes

#26

believe me I tried. you said is not asked by instructions, but if cannot be two GET /json must there else because of previous lesson.
I don’t sent complete

app.get('/json', function (req, res ) {
  if (process.env.MESSAGE_STYLE == "uppercase"){
  message.message= message.message.toUpperCase() ;
 // res.json(message2);
}else{
  //app.get('/json', function (req, res ) {
  message.message= message.message.toLowerCase() ;
//  res.json(message);
    }
  res.json(message);
});/*

I copied your solution and tested by CV, but also negative result
https://atom-earthworm.glitch.me/json
You can try I sent private the link

0 Likes

#27

If I follow the link you sent I get this result:

{ message: "HELLO JSON" }
And this is what is expected.

What result do you get when running your code?

0 Likes

#28

The same, but the freecodecamp CV takes only this

0 Likes

#29

if you click the link the it navigate to solution above in this thread.
I try every possible combinations with same good result as the instruction wanted and
only solution that takes is and if you erase/** 5. because they have same route or make else statement
res.json({"message": "HELLO JSON"}) or {message: "HELLO JSON"}
but
not take this
res.json(JSON.parse(JSON.stringify({message: “HELLO JSON”})))
event this

app.get('/json', (req, res) => {
  if(process.env.MESSAGE_STYLE === 'uppercase'){
     message.message= "HELLO JSON"
    res.json(message);
  }
});
0 Likes

#30

I finally got it to submit after:

  • Commenting out all other code
  • Modified my example to match shimphillip
  • Submitted the url without /json
0 Likes

#31

Hi ;
I’ve read all of the messages in this topic and I want to tell you the solution step by step:
1- You have to go to .env shell and paste this expression: MESSAGE_STYLE=uppercase
the uppercase value will pass as a string so you don’t have to use ’ ’ or " ".

2- In your myApp.js folder introduce a new variable with let that will be equal to a JSON object that has a message value of “Hello json” that is not uppercase. If you don’t do this the tester won’t get the change when your programming is done. let data = {"message": "Hello json"};

3- Add your app.get function that passes the data to "/json’ route in the PATH section and in the METHOD section make a function with (res,req)

app.get('/json', function (req,res) {


});

4- Make an if statement that checks if the environment variable has a special attribute or not. Here our concern is on MESSAGE_STYEL so check the MESSAGE_STYLE like this: if(process.env.MESSAGE_STYLE==="uppercase"){ }

*** You have to put the uppercase case between “” ! Because it passes the string from .env file and you have to get it as a string here. If you don’t use “” you will get -uppercase undefined- error at last.
*** Remember that you have to check the value of the MESSAGE_STYLE with === .
*** For define a special value of an environment variable you have to define it with process.env.VAR_NAME that will be equal to special value. Here Our Value is "uppercase’

5- After your if statement, you have to tell what has to happen. So import this code data.message = data.message.toUpperCase();
*** This code says locate data object -> locate message -> Change the value of the message to uppercase

6- After finishing the if statement, use this code return res.json(data)
****This is important to use return to pass the test because if you don’t, the compiler won’t get the changed value.

ALL OF THE CODE

let data = {"message": "Hello json"};

app.get('/json', function (req,res) {

    if(process.env.MESSAGE_STYLE==="uppercase"){
      data.message = data.message.toUpperCase();
    }
 return res.json(data);
});

Enjoy :slight_smile:

4 Likes

#32

Looks like there are still some problems with this exercise…
I used this code and it didn’t pass:

> let obj = {"message":"Hello json"};
> if(process.env.MESSAGE_STYLE === "uppercase") {
>   obj["message"] = obj["message"].toUpperCase();
> }
> app.get("/json", (req,res) => res.json(obj));

And I added MESSAGE_STYLE=uppercase in .env file
I don’t see where an error could be, if you see please suggest.

And i commented previous lines. Also, it works as it should, even when I change MESSAGE_STYLE in .env file. My only problem is that I can’t pass the test with this code.

0 Likes

#33

There is definitly something wrong with… something… with this challenge. Dont know if it is glitch or freecodecamp but i figured out that even i had valid code it kept falling test, and then after refreshing page and trying with exact same code it worked, and then tried again (no changes) it fails again… So, just double check, always refresh page before submiting, that might be a problem/solution.

ps. Frustration was REAL… hammering submit button for past 2 hours with valid code : )

1 Like

#34

Hi Milan, You have to evaluate process.env.MESSAGE.STYLE inside the get route.
Every time someone makes a get request at /json you check this variable and serve either uppercase text or lowercase.

> let obj = {"message":"Hello json"};
> if(process.env.MESSAGE_STYLE === "uppercase") {
>   obj["message"] = obj["message"].toUpperCase();
> }

The codeblock you wrote outside the route will run once when your server boots up but never again. Imagine you have a function somewhere that changes the value of process.env.MESSAGE_STYLE (this is what is is being tested for as well.)

1 Like

#35

I think it is because testing If the first time is checked it is not o.k ,but when you tested again it is o.k. forever, but when frecodecamp chai tested it return the first result. because there should only one app.get
with res response
and following the tes in 5) and 6) there are two app.get with same route.
the problems can be solved with include previous test 5) with 6) or comment out the test five

0 Likes

#36

Wow, thanks a lot :smiley: I didn’t know that… now i submitted this and it works:


> let obj = {"message":"Hello json"};
> let upperHandler = (req,res) => {
>   if(process.env.MESSAGE_STYLE == "uppercase") {
>     obj["message"] = obj["message"].toUpperCase();
>   }    
>   res.json(obj)
> };
> app.get('/json', upperHandler);
1 Like

#37

FCC seems to add onto their lessons using preceding notes (i.e. check out ‘Basic Node and Express - Serve an HTML File’ and at the bottom the note states:

‘Note: You can edit the solution of the previous challenge, or create a new one. If you create a new solution, keep in mind that Express evaluates the routes from top to bottom. It executes the handler for the first match. You have to comment out the preceding solution, or the server will keep responding with a string.’ )

Basically, at least try commenting out previous solutions if your answer isn’t working.

1 Like

#38

Not sure if I’m beating a dead horse here, but here’s how I got it to work:

Step 1: use MESSAGE_STYLE=“uppercase”; in the .env file

Step 2: Comment out the code from exercise 5, since it’s a route conflict with exercise 6.

Step 3: Use this code in exercise 6:

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

Cheers! :smiley:

0 Likes

#39

This challenge was very frustrating however this passed for me.

Make sure to put MESSAGE_STYLE=“uppercase” in the .env file

app.use("/", (req, res) => {
  let message = "Hello json";
  
  if(process.env.MESSAGE_STYLE==="uppercase"){
    message = message.toUpperCase();
  }
  else{
    message = message;
  }
  return res.json({"message":message})
})
1 Like

#40

// This one solution is one slice-off
// :rofl:
app.get("/json", function(req,res){
let obj = {“message”: “Hello json”};
let myobj = {“message”: “Hello json”};
obj.message=obj.message.toUpperCase();
(process.env.MESSAGE_STYLE==“uppercase”)? res.json(obj) : res.json(myobj);
});
:sunglasses:

0 Likes