Node follow redirects
Drop-in replacement for Nodes http and https that automatically follows redirects, it can also follow http2 redirects.
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 = ;//You can follow http1.1 redirecthttp ;//You can follow http2 / https1.1 redirecthttp2 ;
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;
Options
Global options
Global options are set directly on the node-follow-redirects
module:
const nodeFollowRedirects = ;nodeFollowRedirectsmaxRedirects = 10;nodeFollowRedirectsmaxBodyLength = 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 = ;const http2 = ; const options = url;optionsmaxRedirects = 10;http2;
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 theagent
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 theredirects
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 = ;
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 serverhttp2 ;http2 ;http2 ;