我正在尝试创建一个具有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",
...