Javascript 处理节点上的响应
我有以下功能:Javascript 处理节点上的响应,javascript,node.js,async-await,Javascript,Node.js,Async Await,我有以下功能: function getCookies(response) { console.log(response) const raw = response.headers.raw()['set-cookie']; return raw.map((entry) => { const parts = entry.split(';'); const cookiePart = parts[0]; return cookiePar
function getCookies(response) {
console.log(response)
const raw = response.headers.raw()['set-cookie'];
return raw.map((entry) => {
const parts = entry.split(';');
const cookiePart = parts[0];
return cookiePart;
}).join(';');
}
const _curl = async ({method, URL, headers, body, redirectMode}) => {
let options = {
method: method,
redirect: redirectMode,
credentials: 'include',
headers: headers
}
body ? Object.assign(options, {body: body}) : null
return fetch(URL, ({...options}))
}
我正在向一些站点发送请求,有些站点需要从响应中提取响应和cookie
(async() => {
const getReferCredentials = await // EXTRACT ONLY THE COOKIES FROM THE "SET-COOKIE" HEADER
_curl({
URL: "https://website.com/",
method: "GET",
headers:{
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36",
},
body: false,
redirectMode: "manual"
})
const referCookies = getCookies(getReferCredentials);
cookies = referCookies
const getOnePage = await // EXTRACT RESPONSE BODY AND COOKIES FROM HEADER SET-COOKIE
_curl({
URL: "https://website.com/one-page/",
method: "GET",
headers:{
"coookie": cookies,
},
body: false,
redirectMode: "follow"
}).then((res) => res.text()).then((result) => $ = cheerio.load(result))
const token = $('meta[name=csrf-token]').attr('content');
const checkoutCookies = getCookies(getOnePage ); // Error
cookies = checkoutCookies
});
问题在于getOnePage
函数中,我无法在将变量$作为ChereIO模块实例处理和声明的同时从“SET-COOKIE”头中提取COOKIE。我只能在删除然后
块/处理时提取cookie,就像在函数getreferecredentials
中一样
除了从标题中提取cookie之外,我唯一需要做的就是从页面中提取html响应不要在异步
函数中使用wait
语法可用的then()
方法。你在找什么
const getOnePage = await _curl({
URL: "https://website.com/one-page/",
method: "GET",
headers:{
"coookie": cookies,
},
body: false,
redirectMode: "follow"
});
const checkoutCookies = getCookies(getOnePage);
const result = await getOnePage.text();
const $ = cheerio.load(result);
…
这还修复了$
变量缺少的声明