如何忽略具有循环重定向的页面? 我使用此代码来获取页面:
char
它不会进入'错误'块,我有一个例外:
var libxml = require("libxmljs"),
http = require('follow-redirects').http,
url = require("url");
var request = http.request( { "host": host, "path": URL, "port": 80 }, function( response ) {
var str = '';
response.on( 'data', function( chunk ) {
str += chunk;
});
response.on( 'end', function() {
callback( str, response.statusCode );
}).on( 'error', function ( err ) {
console.log( err );
});
}).end();
答案 0 :(得分:1)
请求对象而非响应对象抛出错误,因此您需要向error
添加(附加)request
侦听器;
var request = http.request(...).on('error', function(err) {
...
}).end();
答案 1 :(得分:0)
查看您正在使用的程序包的文档(https://www.npmjs.com/package/follow-redirects),看起来它只有一个maxRedirects选项。直接来自链接页面:
require('follow-redirects').maxRedirects = 10; // Has global affect (be careful!)
https.request({
host: 'bitly.com',
path: '/UHfDGO',
maxRedirects: 3 // per request setting
}, function (res) {/* ... */});