Basic Node and Express - Serve JSON on a Specific Route

Basic Node and Express - Serve JSON on a Specific Route
0

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 :slight_smile: … 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 ?

Got it resolved!

Thanks everyone!

1 Like

Hello,

I am having a similar issue with this portion of the Node.js section. How were you able to configure the proper code?

i get stuck too bro.

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

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.

1 Like

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!

1 Like

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})
})
1 Like

@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 :wink:

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.