使用Express框架时,如何在Node中下载URL的内容?基本上,我需要完成Facebook身份验证流程,但如果不获取OAuth令牌URL,我就无法做到这一点。
通常,在PHP中,我使用Curl,但Node是等价的?
var options = {
host: 'www.google.com',
port: 80,
path: '/index.html'
};
http.get(options, function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
您将面临的问题是:某些网页使用JavaScript加载其内容。因此,您需要一个包,如After-Load,它模拟浏览器的行为,然后为您提供该URL的HTML内容。
var afterLoad = require('after-load');
afterLoad('https://google.com', function(html){
console.log(html);
});
使用http方式需要更多代码行,只需一个简单的html页面。
这是一种有效的方式:使用请求
var request = require("request");
request({uri: "http://www.sitepoint.com"},
function(error, response, body) {
console.log(body);
});
});
这是请求的文件:https://github.com/request/request
fetch('https://sitepoint.com')
.then(resp=> resp.text()).then(body => console.log(body)) ;