Smaily
July 13, 2021, 5:23pm
1
Hello there
I just finished the challenge of adding a secret file to the project but I couldn’t pass it even my answer is right , it keeps saying " The response of the endpoint ‘/json’ should change according to the environment variable ‘MESSAGE_STYLE’
"
Your project link(s)
you can check the result by visiting :
https://boilerplate-express-1.smailove.repl.co/json
Challenge: Use the .env File
Link to the challenge:
Hey!
The link you added to your code seems to be broken
1 Like
Smaily
July 13, 2021, 5:47pm
5
What do you mean by the first step ? I can’t add anything inside the “function(req,res)”
Sure you can, the handler is just a function. You also do not have to save the value, you can use it directly in the if condition.
BTW, you also have an extra set of parentheses around the handler function.
Smaily
July 13, 2021, 6:01pm
8
Sorry but I don’t know how to add a new handler , I tried this but the server didn’t work .
app.get('/json', (function(req,res) , mySecret {
if( mySecret === "uppercase" ){
res.json({"message" : "hello json".toUpperCase() });
} else {
res.json({"message" : "hello json" });
}
}))
That is not valid syntax. Add the variable assignment inside the function body (between the {}
).
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.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).
1 Like
Smaily
July 13, 2021, 6:10pm
11
But I’ve already added “mySecret” in the if parameter in side the () should I have to declare it again between the {} , because I already did it and nothing changed !
===
is comparison
=
is assignment
Those two do completely different things.
I agree that your syntax is completely invalid.
The handler/callback is just a normal anonymous function.
function(req,res) {
if( mySecret === "uppercase" ){
res.json({"message" : "hello world".toUpperCase() });
} else {
res.json({"message" : "hello world" });
}
}
It works like any other handler/callback function declared inline.
document.addEventListener('click', function() {
...code
})
app.get('/json', function(req, res) {
...code
})
You can also declare it like a normal function and pass it as the handler. With NodeJS/ExpressJS it is often called a controller.
function someHandlerFn() {
...code
}
app.get('/somePath', someHandlerFn);
Summary
function(req,res) {
// assign the variable here
if( mySecret === "uppercase" ){
res.json({"message" : "hello world".toUpperCase() });
} else {
res.json({"message" : "hello world" });
}
}
Smaily
July 13, 2021, 6:24pm
14
Yep I did exactly like this
app.get('/json', (function(req,res) {
mySecret = process.env['MESSAGE_STYLE'];
if( mySecret === "uppercase" ){
res.json({"message" : "hello json".toUpperCase() });
} else {
res.json({"message" : "hello json" });
}
}))
First of all it said " Assignment to constant variable." because the assignment above was a “const” but when I changed it to var it showed the same first message “The response of the endpoint …”
Try declaring mySecret
inside the function.
You have the variable declared at the top, you do not need that anyway. If you declare it inside the function it will be scoped to the function.
You seem to be struggling with some basic syntax and scoping here. ExpressJS is just JS, for the most part, everything you know from plain JS applies.
1 Like
Smaily
July 13, 2021, 6:30pm
16
Still the FFC server couldn’t recognize it although the result appears correctly when I request the end point
It is the strings Hello json
and HELLO JSON
you need to return. Check the case on the first letter for your non uppercase return.
Smaily
July 13, 2021, 6:35pm
18
Here is the result: it works fine :
{“message”:“HELLO JSON”}
The response object should either be {"message": "Hello json"}
or {"message": "HELLO JSON"}
, depending on the MESSAGE_STYLE
value.
Read the required capitalization very carefully. Your responses fail to meet the requirements, specifically your lowercase response.
Smaily
July 13, 2021, 6:46pm
20
I don’t think the problem is there I changed it to ({"message" : "Hello json" })
but I receive the same first message
It is a problem to fail to follow the directions on what the response must be.
I’m not sure why you are still polluting the global space with your mySecret
variable.