服务器不将JSON从Express返回到React(代理)

时间:2017-06-25 00:18:51

标签: json node.js reactjs express proxy

我正在尝试创建一个具有React前端(在端口8080上运行)和Express-Node.js后端(在端口3000上)的应用程序。我希望我的客户端使用fetch从我的服务器请求数据。到目前为止,我在线阅读的内容表明,我需要向proxy条添加package.json条目,其值为http://localhost:3000。我已经完成了这个,我的服务器正确地接收了请求,但它的响应不是我所期望的(一个JSON对象)。我做错了什么?

//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",
...

1 个答案:

答案 0 :(得分:2)

您需要从json

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