我想从控制器获取结果以进行路由,然后渲染将使用NodeJS进行查看。
var request = require('request');
var crypto = require('crypto');
username = "hello@.com.ph";
password = 123124123;
auth = "Basic " + Buffer.from(username + ":" + password).toString("base64");
exports.textname = function(req, res, next) {
request.post({
url : 'https://urlsample/api/getlist',
headers : {
"Authorization" : auth
}
}, function (error, response, body) {
return res.json(body);
});
};
答案 0 :(得分:0)
在上面的代码中,作为第二个参数传递给request.post
的匿名函数是一个回调,因此,您从其返回的内容将被丢弃。要返回响应的正文,您应该在next()
之后调用res.json(body)
:
// code from above...
exports.textname = function(req, res, next) {
request.post({
url : 'https://urlsample/api/getlist',
headers : {
"Authorization" : auth
}
}, function (error, response, body) {
res.json(body);
next();
});
};