2017-06-25 4 views
1

Reactフロントエンド(ポート8080で実行)とExpress-Node.jsバックエンド(ポート3000で)を持つアプリケーションを作成しようとしています。クライアントがfetchを使用してサーバーからデータを要求したいのですが、これまでのところ、私がオンラインで読んだことは、http://localhost:3000の値でpackage.jsonproxyエントリを追加する必要があることを示しています。私はこれを行いました、私のサーバーは要求を正しく受け取りますが、その応答は私が期待するものではありません(JSONオブジェクト)。私は間違って何をしていますか?ExpressからReact(プロキシ)への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

はあなたのresponseのうち、jsonを引っ張っする必要があります。

fetch('/search?crn=10001') 
    .then(response => response.json()) 
    .then(section => console.log(section)); 
関連する問題