Use the .env File

I think Atom is pretty good but I had issues with it slowing way down when I had large Python programs. So I switched to Visual Studio code because it can handle Python and JS. If you’re just doing HTML/CSS/JS, you should be good with Atom. Maybe you can start a new topic and ask for suggestions because there are far better coders out there who have more experience with editors.

This worked for me.

app.get('/json', (req, res) => {
  process.env.MESSAGE_STYLE === 'uppercase' ? 
    res.json({ "message": "HELLO JSON" }):
    res.json({ "message": "Hello json" })
})
10 Likes

It work. Don’t need “return” statement. It will wrong with theory

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

thanx, solved my issue;

Hey guys, the challenge seems bugged:

const messageStyle = process.env.MESSAGE_STYLE;
const message = messageStyle === 'uppercase' ? 'HELLO JSON' : 'Hello json';
app.get('/json', (request, response) => response.json({message}));

this doesn’t work. but will change according to the variable set in .env. I get the right result. If you are not familiar, if the name of your key and value pairing match, you don’t need to specify the key. The output of the code is: {"message": "HELLO JSON"}

Hi, I have struggled with this for a while. It was working for me, but would not pass the test.

Turn out, the key thing to pass this test is to check for env variable value inside the response function.

so, this was working for me, but not passing test (in other words, changing variable value in the .env file was yielding correct results)

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

However, you need to check the condition inside the response function:

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

This is correctly passing the test for this exercise.

2 Likes

Hello Friends, If you have problem on this challenge then read these carefully.

  1. FCC doesn’t provide .env file with their template. So just create new .env file from New FIle dropdown. Then paste this code there: MESSAGE_STYLE=uppercase .
  2. Next in your app.js, paste this code:
var express = require('express');
var app = express();
console.log("Hello World")

app.get("/", function(req, res){
  res.sendFile(__dirname+ '/views/index.html');
  app.use(express.static(__dirname + "/public"));
})
app.get("/json", (req, res) => {
     if(process.env.MESSAGE_STYLE === "uppercase"){
       res.json({"message" : "HELLO JSON"});
     }
     res.json({"message" : "Hello json"});
   });
module.exports = app;

YOUR APP.JS SHOULD ONLY CONTAIN MY PROVIDED CODE, NOTHING ELSE (except comments)

3 Likes

after writing this code

should i change url with “/json” in the end…??

it stil deosnt work by your solution of creating new env file n all

1 Like

to pass this test you should create a new env file. Just click “new File” and with no name just type “.env”, create this file and state there MESSAGE_STYLE=uppercase;
image

3 Likes

app.get("/json",(req,res)=>{process.env.MESSAGE_STYLE==‘uppercase’?res.json({“message”: “Hello json”.toUpperCase()}) : res.json({“message”: “Hello json”})});
use only one app.get this problem was headache.

1 Like

None of the solutions given here worked for me tried a million times.

@yaser, milion times! hahaha . So what are you planning now?

If you really tried all the solutions here, and not working for you----
I would suggest, stop working on the current file and start again with a new link for this challenge. Before taking this decision, you can take a break for few hours and try to solve this for few more times by following the challenge instructions. Moreover, you can share your code here for a review. :grinning:

Thank you you were right, i needed a break and stop pushing myself it was a tiny mistake. Had I rested I would have saved much energy and effort.

1 Like

This is the only solution that works.

It appears that the .env file must be added with MESSAGE_STYLE=uppercase being the only code written in the file.

Everyone else’s solution previously mentioned in this app should work. It worked for me.

This one perfectly works for me.

I haven’t created this .env file with MESSAGE_STYLE=uppercase variable at first

all solutions do not work. including opening .env file with a variable in it.

Could you check my code on glitch. I can’t find any issue with it but it doesn’t pass the test.
https://glitch.com/edit/#!/tar-calico-curio

.env file contains only MESSAGE_STYLE=uppercase

Edit:

Looks like my code is fine, and I bet yours is also fine. It’s issue on glitch’s side.
I found this: http://forum.freecodecamp.org/t/node-and-express-tests-failing/406988
I’ve just moved my code from glitch to repl.it and finally challenge is done. Shame I wasted so much time today.

It also happened with me i wasted a whole day on this bug but repl.it looks better anyways

how did you tested your code ?