npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-follow-redirects

v1.0.1

Published

HTTP2 , HTTP and HTTPS modules that follow redirects

Downloads

12

Readme

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