【发布时间】:2017-11-28 06:03:05
【问题描述】:
我正在尝试制作一个具有 React 前端(在端口 8080 上运行)和 Express-Node.js 后端(在端口 3000 上)的应用程序。我想让我的客户使用fetch 从我的服务器请求数据。到目前为止,我在网上看到的内容表明我需要在我的package.json 中添加一个proxy 条目,其值为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",
...
【问题讨论】:
标签: json node.js reactjs express proxy