sadewow
October 24, 2020, 7:56pm
#1
Tell us what’s happening:
After a lot of attempts, I still get this message. Please help me
The response of the endpoint /json
should change according to the environment variable MESSAGE_STYLE
Your code so far
process.env.MESSAGE_STYLE = "uppercase";
if (process.env.MESSAGE_STYLE == "uppercase") {
app.get('/json', function(req, res) {
res.json.message.toUpperCase();
})
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 OPR/71.0.3770.284
.
Challenge: Use the .env File
Link to the challenge:
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.
By hardcoding with this line, your code will always make the message uppercase regardless of what is actually in the .env
file. Make the changes in the .env
file.
Sky020
October 24, 2020, 8:32pm
#3
Hello there,
You need to create a .env
file, and place MESSAGE_STYLE=uppercase
in it. No ending ;
, and no spaces between the =
. Also, the logic (if
) should be within the anonymous function within the GET
handle.
Otherwise, please note: It is difficult for us to help further without seeing more of your code. Ideally, could you please share the link to your project code?
sadewow
October 25, 2020, 1:51pm
#4
I’m sorry, I still don’t get it. Here’s my link
[My solution link](https://repl.it/@sadewow/boilerplate-express#myApp.js)
As I know the process.env is creating .env file, isn’t it?
sadewow
October 25, 2020, 1:54pm
#5
Does it mean I need to create
process.env.MESSAGE_STYLE
Then I assign to
process.env.MESSAGE_STYLE = “uppercase”
?
Sky020
October 25, 2020, 2:23pm
#6
No. You need to create a file with the name .env
(the dot must be included)
This is a special file. Read more about it here: https://www.freecodecamp.org/news/heres-how-you-can-actually-use-node-environment-variables-8fdf98f53a0a/
Then, you need to create a variable in it like this:
MESSAGE_STYLE=uppercase
To access the value of a variable in a .env
file, you use:
process.env.VARIABLE_NAME
Also, you should not have 2 /json
routes. The instructions are:
Then tell the GET /json
route handler that you created in the last challenge…
Hope this clarifies.
1 Like
sadewow
October 25, 2020, 6:25pm
#7
At first the terminal seems ok, and client can load the file.
And then I got lof of error and client going down due to errors.
This is my code
app.get('/json', function(req, res) {
if (process.env.MESSAGE_STYLE === uppercase) {
res.json({"message": "Hello json".toUpperCase()})
}
})
I also imported the dotenv package based on the tutorial
Sky020
October 25, 2020, 6:47pm
#8
Hello there,
You are not quite satisfying this:
… to transform the response object’s message to uppercase if process.env.MESSAGE_STYLE
equals uppercase
.
There should always be a response…
sadewow
October 25, 2020, 6:57pm
#9
Hi,
I’d changed my code to
app.get('/json', function(req, res) {
if (process.env.MESSAGE_STYLE === 'uppercase') {
res.json({"message": "Hello json".toUpperCase()})
}
})
The error say sytanx error.
SyntaxError: Unexpected token s in JSON at position 0
at JSON.parse (<anonymous>)
at /home/runner/boilerplate-express/node_modules/fcc-express-bground/index.js:91:26
at IncomingMessage.<anonymous> (/home/runner/boilerplate-express/node_modules/fcc-express-bground/index.js:33:7)
May I know the incorrect syntax is?
I’m very confused here, sorry.
Sky020
October 25, 2020, 7:52pm
#10
Your /json
route must always respond with something. Currently, it is only responding, when process.env.MESSAGE_STYLE === 'uppercase'
1 Like
sadewow
October 25, 2020, 9:11pm
#11
Is this not a response?
res.json.message.toUpperCase()
Sky020
October 25, 2020, 9:13pm
#12
The above is not. What you have here is:
res.json({"message": "Hello json".toUpperCase()})
However, I will reiterate:
What are you responding with, when process.env.MESSAGE_STYLE !== 'uppercase'
?
1 Like
sadewow
October 25, 2020, 9:21pm
#13
I get it Sir. Thank you very much!
lasjorg
October 25, 2020, 9:22pm
#14
What link are you submitting?
Sky020
October 25, 2020, 9:28pm
#15
I assume the error was coming from the tests trying to parse a JSON response from a route which did not respond with anything.