Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including node-follow-redirects with all npm packages installed. Try it out:

var nodeFollowRedirects = require("node-follow-redirects")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

node-follow-redirects v1.0.1

HTTP2 , HTTP and HTTPS modules that follow redirects

Node follow redirects

Drop-in replacement for Nodes http and https that automatically follows redirects, it can also follow http2 redirects.

NPM Version Build Status Known Vulnerabilities

node-follow-redirects provides mimic the native http and https modules, with the exception these will seamlessly follow redirects in http1.1 / https1.1 / http2 . It will automatically use https1.1 / http2 when needed according to server protocol. To be http2 compatible it uses http2-client. To follow redirects it uses follow-redirects - expect the API to be identical just with http2 support.

const {http , https , http2} = require('node-follow-redirects');
//You can follow http1.1 redirect
http.get('http://bit.ly/900913', (response) =>{
        response.on('data',  console.log );
    })
    .on('error', console.error );
//You can follow http2 / https1.1 redirect
http2.get('http://bit.ly/900913', (response) =>{
        response.on('data',  console.log );
    })
    .on('error', console.error );

You can inspect the final redirected URL through the responseUrl property on the response. If no redirection happened, responseUrl is the original request URL.

http2.request({
  host: 'bitly.com',
  path: '/UHfDGO',
}, function (response) {
  console.log(response.responseUrl);
  // 'http://duckduckgo.com/robots.txt'
});

Options

Global options

Global options are set directly on the node-follow-redirects module:

const nodeFollowRedirects = require('node-follow-redirects');
nodeFollowRedirects.maxRedirects = 10;
nodeFollowRedirects.maxBodyLength = 20 * 1024 * 1024; // 20 MB

The following global options are supported:

  • maxRedirects (default: 21) – sets the maximum number of allowed redirects; if exceeded, an error will be emitted.

  • maxBodyLength (default: 10MB) – sets the maximum size of the request body; if exceeded, an error will be emitted.

Per-request options

Per-request options are set by passing an options object:

const url = require('url');
const {http2} = require('node-follow-redirects');

const options = url.parse('https://bit.ly/900913');
options.maxRedirects = 10;
http2.request(options);

In addition to the standard HTTP and HTTPS options, the following per-request options are supported:

  • followRedirects (default: true) – whether redirects should be followed.

  • maxRedirects (default: 21) – sets the maximum number of allowed redirects; if exceeded, an error will be emitted.

  • maxBodyLength (default: 10MB) – sets the maximum size of the request body; if exceeded, an error will be emitted.

  • agents (default: undefined) – sets the agent option per protocol, since HTTP and HTTPS use different agents. Example value: { http: new http.Agent(), https: new https.Agent() }

  • trackRedirects (default: false) – whether to store the redirected response details into the redirects array on the response object.

Advanced usage

By default, node-follow-redirects will use the Node.js default implementations of http and https or http2-client . To enable features such as caching and/or intermediate request tracking, you might instead want to wrap node-follow-redirects around custom protocol implementations:

var followRedirects = require('node-follow-redirects').wrap({
  http: require('your-custom-http'),
  https: require('your-custom-https'),
});

Such custom protocols only need an implementation of the request method.

Features

Transparently supports all http protocol.

  • Http/1.1
  • Https/1.1
  • Http/2.0

In case of http1.1 the connection pool is managed as usual with an http agent. In case of http2 all requests use a signle connection (per domain/port ).

//The following will create a single http2 connection to the server
http2.get('https://www.google.com', (response) =>{
        response.on('data',  console.log );
    })
    .on('error', console.error );
http2.get('https://www.google.com', (response) =>{
        response.on('data',  console.log );
    })
    .on('error', console.error );
http2.get('https://www.google.com', (response) =>{
        response.on('data',  console.log );
    })
    .on('error', console.error );

License

MIT

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free