2017-06-25 25 views
1

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

回答

2

你需要拉json你的response的:

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