#javascript #node.js #promise
#javascript #node.js #обещание
Вопрос:
У меня есть эта функция, которая считывает CSV-файл, содержащий список URL-адресов. Для каждой строки я делаю запрос get на текущий URL-адрес, чтобы прочитать заголовок ответа.
function readCSV(csv) {
var lines = csv.split("n");
var table = lines.map((line) => line.split(","));
var requests = table.map((row) =>
request({
method: "GET",
uri: "https://www." row[1],
resolveWithFullResponse: true,
})
.catch((err) => null) // Errors are ignored and resolved as `null`
);
return Promise.all(requests)
.then((responses) => {
responses.forEach((response) => {
if(response === null) return; // If the response is null, skip it
// ... handle successful responses here
var hrds = response.headers;
//console.log(hrds.url)
console.log(hrds['content-security-policy'])
console.log(hrds['x-frame-options'])
});
})
.catch((err) => console.log(err));
}
Я хотел бы сделать что-то вроде: console.log(hrds.url)
чтобы увидеть URL, который отправил мне ответ. Если я попытаюсь распечатать hrds.url
, это даст мне undefined.
РЕДАКТИРОВАТЬ: я пытался response.url
, но он печатает пустую строку
Ответ №1:
Согласно MDN docs, вы можете получить URL-адрес из самого ответа.
response.url
Комментарии:
1. можете ли вы
console.log(response)
и поделиться здесь?2. Я не могу опубликовать его, он печатает весь заголовок и тело, но строка слишком длинная, даже если я использую только один URL.