| 1 | ## Follow Redirects
|
|---|
| 2 |
|
|---|
| 3 | Drop-in replacement for Node's `http` and `https` modules that automatically follows redirects.
|
|---|
| 4 |
|
|---|
| 5 | [](https://www.npmjs.com/package/follow-redirects)
|
|---|
| 6 | [](https://github.com/follow-redirects/follow-redirects/actions)
|
|---|
| 7 | [](https://coveralls.io/r/follow-redirects/follow-redirects?branch=master)
|
|---|
| 8 | [](https://www.npmjs.com/package/follow-redirects)
|
|---|
| 9 | [](https://github.com/sponsors/RubenVerborgh)
|
|---|
| 10 |
|
|---|
| 11 | `follow-redirects` provides [request](https://nodejs.org/api/http.html#http_http_request_options_callback) and [get](https://nodejs.org/api/http.html#http_http_get_options_callback)
|
|---|
| 12 | methods that behave identically to those found on the native [http](https://nodejs.org/api/http.html#http_http_request_options_callback) and [https](https://nodejs.org/api/https.html#https_https_request_options_callback)
|
|---|
| 13 | modules, with the exception that they will seamlessly follow redirects.
|
|---|
| 14 |
|
|---|
| 15 | ```javascript
|
|---|
| 16 | const { http, https } = require('follow-redirects');
|
|---|
| 17 |
|
|---|
| 18 | http.get('http://en.wikipedia.org/', response => {
|
|---|
| 19 | response.on('data', chunk => {
|
|---|
| 20 | console.log(chunk);
|
|---|
| 21 | });
|
|---|
| 22 | }).on('error', err => {
|
|---|
| 23 | console.error(err);
|
|---|
| 24 | });
|
|---|
| 25 | ```
|
|---|
| 26 |
|
|---|
| 27 | You can inspect the final redirected URL through the `responseUrl` property on the `response`.
|
|---|
| 28 | If no redirection happened, `responseUrl` is the original request URL.
|
|---|
| 29 |
|
|---|
| 30 | ```javascript
|
|---|
| 31 | const request = https.request({
|
|---|
| 32 | host: 'en.wikipedia.org',
|
|---|
| 33 | path: '/',
|
|---|
| 34 | }, response => {
|
|---|
| 35 | console.log(response.responseUrl);
|
|---|
| 36 | // 'http://duckduckgo.com/robots.txt'
|
|---|
| 37 | });
|
|---|
| 38 | request.end();
|
|---|
| 39 | ```
|
|---|
| 40 |
|
|---|
| 41 | ## Options
|
|---|
| 42 | ### Global options
|
|---|
| 43 | Global options are set directly on the `follow-redirects` module:
|
|---|
| 44 |
|
|---|
| 45 | ```javascript
|
|---|
| 46 | const followRedirects = require('follow-redirects');
|
|---|
| 47 | followRedirects.maxRedirects = 10;
|
|---|
| 48 | followRedirects.maxBodyLength = 20 * 1024 * 1024; // 20 MB
|
|---|
| 49 | ```
|
|---|
| 50 |
|
|---|
| 51 | The following global options are supported:
|
|---|
| 52 |
|
|---|
| 53 | - `maxRedirects` (default: `21`) – sets the maximum number of allowed redirects; if exceeded, an error will be emitted.
|
|---|
| 54 |
|
|---|
| 55 | - `maxBodyLength` (default: 10MB) – sets the maximum size of the request body; if exceeded, an error will be emitted.
|
|---|
| 56 |
|
|---|
| 57 | ### Per-request options
|
|---|
| 58 | Per-request options are set by passing an `options` object:
|
|---|
| 59 |
|
|---|
| 60 | ```javascript
|
|---|
| 61 | const url = require('url');
|
|---|
| 62 | const { http, https } = require('follow-redirects');
|
|---|
| 63 |
|
|---|
| 64 | const options = url.parse('http://en.wikipedia.org/');
|
|---|
| 65 | options.maxRedirects = 10;
|
|---|
| 66 | options.beforeRedirect = (options, response, request) => {
|
|---|
| 67 | // Use this to adjust the request options upon redirecting,
|
|---|
| 68 | // to inspect the latest response headers,
|
|---|
| 69 | // or to cancel the request by throwing an error
|
|---|
| 70 |
|
|---|
| 71 | // response.headers = the redirect response headers
|
|---|
| 72 | // response.statusCode = the redirect response code (eg. 301, 307, etc.)
|
|---|
| 73 |
|
|---|
| 74 | // request.url = the requested URL that resulted in a redirect
|
|---|
| 75 | // request.headers = the headers in the request that resulted in a redirect
|
|---|
| 76 | // request.method = the method of the request that resulted in a redirect
|
|---|
| 77 | if (options.hostname === "example.org") {
|
|---|
| 78 | options.auth = "user:password";
|
|---|
| 79 | }
|
|---|
| 80 | };
|
|---|
| 81 | http.request(options);
|
|---|
| 82 | ```
|
|---|
| 83 |
|
|---|
| 84 | In addition to the [standard HTTP](https://nodejs.org/api/http.html#http_http_request_options_callback) and [HTTPS options](https://nodejs.org/api/https.html#https_https_request_options_callback),
|
|---|
| 85 | the following per-request options are supported:
|
|---|
| 86 | - `followRedirects` (default: `true`) – whether redirects should be followed.
|
|---|
| 87 |
|
|---|
| 88 | - `maxRedirects` (default: `21`) – sets the maximum number of allowed redirects; if exceeded, an error will be emitted.
|
|---|
| 89 |
|
|---|
| 90 | - `maxBodyLength` (default: 10MB) – sets the maximum size of the request body; if exceeded, an error will be emitted.
|
|---|
| 91 |
|
|---|
| 92 | - `beforeRedirect` (default: `undefined`) – optionally change the request `options` on redirects, or abort the request by throwing an error.
|
|---|
| 93 |
|
|---|
| 94 | - `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() }`
|
|---|
| 95 |
|
|---|
| 96 | - `trackRedirects` (default: `false`) – whether to store the redirected response details into the `redirects` array on the response object.
|
|---|
| 97 |
|
|---|
| 98 | - `sensitiveHeaders` (default: `[]`) – names of headers to omit when making redirected requests (such as `X-API-Key`, `X-Auth-Token`…)
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 | ### Advanced usage
|
|---|
| 102 | By default, `follow-redirects` will use the Node.js default implementations
|
|---|
| 103 | of [`http`](https://nodejs.org/api/http.html)
|
|---|
| 104 | and [`https`](https://nodejs.org/api/https.html).
|
|---|
| 105 | To enable features such as caching and/or intermediate request tracking,
|
|---|
| 106 | you might instead want to wrap `follow-redirects` around custom protocol implementations:
|
|---|
| 107 |
|
|---|
| 108 | ```javascript
|
|---|
| 109 | const { http, https } = require('follow-redirects').wrap({
|
|---|
| 110 | http: require('your-custom-http'),
|
|---|
| 111 | https: require('your-custom-https'),
|
|---|
| 112 | });
|
|---|
| 113 | ```
|
|---|
| 114 |
|
|---|
| 115 | Such custom protocols only need an implementation of the `request` method.
|
|---|
| 116 |
|
|---|
| 117 | ## Browser Usage
|
|---|
| 118 |
|
|---|
| 119 | Due to the way the browser works,
|
|---|
| 120 | the `http` and `https` browser equivalents perform redirects by default.
|
|---|
| 121 |
|
|---|
| 122 | By requiring `follow-redirects` this way:
|
|---|
| 123 | ```javascript
|
|---|
| 124 | const http = require('follow-redirects/http');
|
|---|
| 125 | const https = require('follow-redirects/https');
|
|---|
| 126 | ```
|
|---|
| 127 | you can easily tell webpack and friends to replace
|
|---|
| 128 | `follow-redirect` by the built-in versions:
|
|---|
| 129 |
|
|---|
| 130 | ```json
|
|---|
| 131 | {
|
|---|
| 132 | "follow-redirects/http" : "http",
|
|---|
| 133 | "follow-redirects/https" : "https"
|
|---|
| 134 | }
|
|---|
| 135 | ```
|
|---|
| 136 |
|
|---|
| 137 | ## Contributing
|
|---|
| 138 |
|
|---|
| 139 | Pull Requests are always welcome. Please [file an issue](https://github.com/follow-redirects/follow-redirects/issues)
|
|---|
| 140 | detailing your proposal before you invest your valuable time. Additional features and bug fixes should be accompanied
|
|---|
| 141 | by tests. You can run the test suite locally with a simple `npm test` command.
|
|---|
| 142 |
|
|---|
| 143 | ## Debug Logging
|
|---|
| 144 |
|
|---|
| 145 | `follow-redirects` uses the excellent [debug](https://www.npmjs.com/package/debug) for logging. To turn on logging
|
|---|
| 146 | set the environment variable `DEBUG=follow-redirects` for debug output from just this module. When running the test
|
|---|
| 147 | suite it is sometimes advantageous to set `DEBUG=*` to see output from the express server as well.
|
|---|
| 148 |
|
|---|
| 149 | ## Authors
|
|---|
| 150 |
|
|---|
| 151 | - [Ruben Verborgh](https://ruben.verborgh.org/)
|
|---|
| 152 | - [Olivier Lalonde](mailto:olalonde@gmail.com)
|
|---|
| 153 | - [James Talmage](mailto:james@talmage.io)
|
|---|
| 154 |
|
|---|
| 155 | ## License
|
|---|
| 156 |
|
|---|
| 157 | [MIT License](https://github.com/follow-redirects/follow-redirects/blob/master/LICENSE)
|
|---|