我正在使用node.js和node-fetch模块。
我想要一个带有url并返回html内容以供以后处理的函数。
问题是我无法获取返回html的代码。这是功能。您会注意到该函数将主体记录到控制台。如何从函数中返回该值?
function getHtml(myUrl) {
return new Promise(function(resolve, reject) {
fetch(myUrl)
.then(res => res.text())
.then(body => console.log(body));
});
}
在Bergi发表评论后,我尝试了一下,但是没有运气:
function getHtml(myUrl) {
return fetch(myUrl)
.then(res => res.text())
.then(body => body.text());
}