Hi Campers
I was just going through new curriculum and for some reason, this code is not going through for me on this challenge.
Challenge: https://learn.freecodecamp.org/apis-and-microservices/basic-node-and-express/serve-json-on-a-specific-route
Code that I have for this challenge
app.get(function(req, res, next) {
console.log('Hello json');
fs.readFile(__dirname + '/package.json', function(err, data) {
if(err) return next(err);
res.type('txt').send(data.toString());
});
});
Did I make some silly boo-boo that I am not noticing ?
Thanks for help in advance.
Updated code
app.get(function(req, res, next) {
console.log('Hello json');
fs.readFile('/package.json', function(err, data) {
if(err) return next(err);
res.type('txt').send(data.toString());
});
});
Still not working
Saying not found
@BoCode84 what is saying is not found?
@Mortuie
This is what I see
// running test
Not Found
// tests completed
@BoCode84 I see one error which is you havenāt specified a route.
i.e.
app.get('/json', function(req, res, next) {
........
}
I assume that is why it is saying not found as you havenāt specified a route to serve. Everything else seems fine!
I knew there was something ā¦ thanks.
Now itās showing me different error
// running test
ENOENT: no such file or directory, open '/package.json'
// tests completed
1 Like
Still getting error ; anyone ?
Hello,
I am having a similar issue with this portion of the Node.js section. How were you able to configure the proper code?
can you tell me the solution
@Devilcode007 & @dhahn1
app.get('/json', (req, res) => {
let message = 'Hello json'
if (process.env.MESSAGE_STYLE === 'uppercase') {
return res.json({"message": message.toUpperCase()})
}
return res.status(200).json({"message": message})
})
This is my solution ; you might have to check your whole code to see if this solution fits your code
1 Like
app.get(ā/jsonā,function(req,res)
{
let message=āHello jsonā;
res.status(200).setHeader(āContent-Typeā, āapplication/jsonā);
res.json({āmessageā: message});
});
if you use the Glitch, itās very simple to pass the test as below.
2 Likes
Iām getting no found here as hell, even if I can show the JSON response on the browser using the [glitch_url]/json. I assume is a testing problem as it works perfectly for me.
https://platinum-windscreen.glitch.me/json
app.get("/json", function(req, res) {
let message = 'Hello json'
if (process.env.MESSAGE_STYLE === 'uppercase') {
return res.status(200).json({"message": message.toUpperCase()})
}
return res.status(200).json({"message": message})
})
.env file is
MESSAGE_STYLE="uppercase"
One must provide his/her app URL without ā/jsonā for passing the challenge i.e., in my case it is - https://outgoing-toast.glitch.me and not the URL of page where you are serving JSON response (https://outgoing-toast.glitch.me/json ).
Hope it helps!
6 Likes
A simple answer to this challenge, similar to that of @ccrubby214 above:
app.get("/json", (req, res) => {
res.json({"message": "Hello json"})
})
Iām wondering if the tester isnāt working. I have tried all your guys solutions and have got nothing. I thought it maybe was the space after āmessageā: āHello jsonā that was causing the issue but it is not. Please help:
https://axiomatic-athlete.glitch.me/json
https://axiomatic-athlete.glitch.me/
This is resolved. After going on to the next lesson, I was able to find deeper discussion on the same issue. This is what I used to solve this lesson:
app.use('/json', (req, res) => {
let message = "Hello json"
return res.json({"message": message})
})
2 Likes
@gauravano , YOU, my friend, had the answer which saved me from going [completely] bald!! I could have saved myself HOURS of frustration had I found your answer earlier. This worked for me. THANK YOU!! Youāre an absolute genius, kind sir
PatChe
May 16, 2019, 12:42pm
20
Thanks. Spent too long trying to ensure the test url fit the proposed and presumed URI for the testing. Doesnāt seem logical that the test should be at the root seeing that the instruction is proposing a reroute.
Perhaps FCC should clarify on the challenge page.