2017-01-04 4 views
0

サンプルコード:サーバー側のAPIからデータを取得し、取得したデータをexpressjsを使用してpugjsファイルに渡す方法はありますか?

// index.pug

p #{polls} 

// apiendpoint

http://localhost:8080/api/polls 

// routesファイル(index.js):ここで

、私はどのように行いますapiにリクエストを行い、プロファイルをレンダリングしながらapi(locals)の検索結果を変数pollsに渡します。

app.route('/profile') 
     .get(isLoggedIn, function (req, res) { 

      res.render('profile', {'polls': passvaluehere}); 
      }); 

     }); 

答えて

2
You can also use **http** module like this 

var http = require('http'); 
var options = { 
    host: 'localhost', 
    path: '/api/polls', 
    port: '80', 
    method: 'GET' 
}; 


var req = http.request(options, response); 
var str = '' 
    response.on('data', function (chunk) { 
    str += chunk; 
    }); 

    response.on('end', function() { 
    console.log(str); 
    res.render('profile', {'polls': str}); 
    }); 
req.end(); 
関連する問題