Tell us what’s happening:
When I run test I get CORS error
Access to XMLHttpRequest at 'https://outgoing-viola.glitch.me/_api/use-env-vars' from origin 'https://www.freecodecamp.org' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
jquery.js:10099 GET https://outgoing-viola.glitch.me/_api/use-env-vars net::ERR_FAILED
How can I pass the test?
Is that enough to submit a bug?
Your code so far
Glitch project editor
My glitch project
.env, App.js Solutions
.env
# Environment Config
MESSAGE_STYLE=uppercase
# store your secrets and config variables in here
# only invited collaborators will be able to see your .env values
# reference these in your code with process.env.SECRET
SECRET=
MADE_WITH=
app.get('/json', (req,res)=>{
if(process.env.MESSAGE_STYLE==='uppercase'){
res.json({"message": "HELLO JSON"});
}
res.json({"message": "Hello json"});
});
app.get( '/json', (req, res)=>{
let message = 'Hello json';
if(process.env.MESSAGE_STYLE==='uppercase'){
res.json({message: message.toUpperCase()});
} else{
res.json({message: message});
}
});
app.get('/json', (req,res)=>{
const jsonObj = {"message": "Hello json"}
if(process.env.MESSAGE_STYLE==="uppercase"){
res.json({"message": jsonObj.message.toUpperCase()});
}
res.json(jsonObj);
});
app.get('/json', (req,res)=>{
if(process.env.MESSAGE_STYLE==='uppercase'){
res.json({message: "HELLO JSON"});
}
res.json({message: "Hello json"});
});
app.get('/json', (req,res)=>res.json(process.env.MESSAGE_STYLE==='uppercase'?{message: "Hello json"}:{"message": "HELLO JSON"});
For pieces of code above I commented out all previous solutions, here I tried to combine 2, just in case they are connected somehow
/** 5) serve JSON on a specific route */
const json = {"message": "Hello json"};
app.get('/json', (req,res)=>{
res.json(json);
});
/** 6) Use the .env file to configure the app */
if(process.env.MESSAGE_STYLE==='uppercase') json.message=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/83.0.4103.61 Safari/537.36
.
Challenge: Use the .env File
Link to the challenge: