将数据从请求对象传递到模板 node.js

Pass data from request object to template node.js

我正在使用 request 包来发出 GET 请求。我试图将请求中的数据作为回调传递到我的模板,但我一直收到错误 res.render is not a function。这是我的代码:

module.exports = function (req, res) {
request("http://localhost:3000/api/employee", function(err, res, body) {

    var apiResponse = JSON.parse(body);
    var context = {
        name: apiResponse[0].first_name
    }
    res.render('../views/home', context);
});
};

有人可以帮忙吗?

提前致谢!

module.exports = function (req, res) {
request("http://localhost:3000/api/employee", function(err, response, body) {

    var apiResponse = JSON.parse(body);
    var context = {
        name: apiResponse[0].first_name
    }
    res.render('../views/home', context);
});
};

请使用此代码。 您的代码中的问题是有两个 res 变量,res.render 应该使用第一个 res.