2017-06-25 77 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));