Quontas
Quontas

Reputation: 399

Server not returning JSON from Express to React (proxy)

I'm attempting to make an application that has a React frontend (running on port 8080) and an Express-Node.js backend (on port 3000). I'd like for my client to use fetch to request data from my server. So far, what I've read online indicates that I need to add a proxy entry to my package.json with the value of http://localhost:3000. I've done this, my server receives the request correctly, but its response is not what I expect (a JSON object). What am I doing wrong?

//Server
app.get('/search', function(req, res) {
...
      //console.log(section) <-- Is the correct value
      res.json(section);
    })
...
app.listen(3000)


//Client
  handleTouchTap() {
    fetch('/search?crn=10001').then(function(response) { //<-- Hard-coded for testing
      return response; //<-- Does not contain the value of "section" from server
    }).then(function(data) {
            console.log(data); //<-- Likewise, does not contain the value
    });
  }

//From package.json
...
    "proxy": "http://localhost:3000",
...

Upvotes: 1

Views: 724

Answers (1)

Andy Gaskell
Andy Gaskell

Reputation: 31761

You need to pull the json out of your response:

fetch('/search?crn=10001')
  .then(response => response.json())
  .then(section => console.log(section));

Upvotes: 2

Related Questions