Basic Node and Express - Serve JSON on a Specific Route

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

#1

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.


#2

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


#3

@BoCode84 what is saying is not found?


#4

@Mortuie

This is what I see

// running test
Not Found
// tests completed

#5

@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!


#6

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

#7

Still getting error ; anyone ?


#8

Got it resolved!

Thanks everyone!


#9

Hello,

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


#10

i get stuck too bro.


#11

can you tell me the solution


#12

@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


#13

app.get(’/json’,function(req,res)
{
let message=“Hello json”;
res.status(200).setHeader(‘Content-Type’, ‘application/json’);

res.json({“message”: message});

});


#14

if you use the Glitch, it’s very simple to pass the test as below.


#15

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"

#16

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!


#17

A simple answer to this challenge, similar to that of @ccrubby214 above:

app.get("/json", (req, res) => {
  res.json({"message": "Hello json"})
})