Aleksey Solomakha
Aleksey Solomakha

Reputation: 113

Node.js Error: Max redirects exceeded

how I can ignore pages with cycle redirects? I use this code to fetching pages:

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();

It will not go to 'error' block, and I've got an exception:

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: Max redirects exceeded.
    at ClientRequest.cb (/var/parsing/node_modules/follow-redirects/create.js:55:19)
    at ClientRequest.g (events.js:199:16)
    at ClientRequest.emit (events.js:107:17)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:426:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
    at Socket.socketOnData (_http_client.js:317:20)
    at Socket.emit (events.js:107:17)
    at readableAddChunk (_stream_readable.js:163:16)
    at Socket.Readable.push (_stream_readable.js:126:10)
    at TCP.onread (net.js:538:20)

Upvotes: 1

Views: 13704

Answers (2)

robertklep
robertklep

Reputation: 203494

The error is being thrown by the request object, not the response object, so you need to add an (additional) error listener to request;

var request = http.request(...).on('error', function(err) {
  ...
}).end();

Upvotes: 1

kbjr
kbjr

Reputation: 1254

Looking at the docs for the package you are using (https://www.npmjs.com/package/follow-redirects), it looks like it just has a maxRedirects option. Directly from the linked page:

require('follow-redirects').maxRedirects = 10;   // Has global affect (be careful!) 

https.request({
  host: 'bitly.com',
  path: '/UHfDGO',
  maxRedirects: 3   // per request setting 
}, function (res) {/* ... */});

Upvotes: 1

Related Questions