0

示例代码:

// index.pug

p #{polls}

// 端点

http://localhost:8080/api/polls

// 路由文件 (index.js):

在这里,我如何向 api 发出 get 请求,并在渲染 profile.pug 时将检索到的结果从 api(locals) 传递给 polls 变量

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

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

        });
4

1 回答 1

1
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();
于 2017-01-04T18:45:01.510 回答