this.httpXsrf = function() {
var token;
var http = require("https");
var options = {
"method" : "GET",
"hostname" : "infomatrix-ci.cmxdev.com",
"port" : null,
"path" : "/",
"headers" : {
"cache-control" : "no-cache",
}
};
http.request(options, function(res) {
var chunks = [];
var setcookie = res.headers["set-cookie"];
if (setcookie) {
setcookie.forEach(
function(cookiestr) {
if (cookiestr.indexOf('XSRF-TOKEN') > -1) {
var a = cookiestr.split(';');
token = a[1].splice;
}
});
}
res.on("data", function(chunk) {
chunks.push(chunk);
});
res.on("end", function() {
var body = Buffer.concat(chunks);
console.log(body.toString());
});
}).end();
};
在上面的代码中,我想从函数返回令牌变量并将其存储在测试页面上的antoher变量中, 等,
var token = apiPage.httpxsrf();
或
apiPage.httpxsrf().then(function(token){
// call another function like apiPage.httplogin(token);
});
如何在变量中返回令牌和存储并使用令牌变量调用另一个函数?