Follow Redirects——模具行业从业者必备知识手册
-
<h3>Follow Redirects</h3>
<p>Drop-in replacement for Node's <code>http</code> and <code>https</code> modules that automatically follows redirects.</p>
<p><a href="upload://u6mWk1mXA1u3u5hkb4wBWmtW220.svg"><img src="https://www.npmjs.com/package/follow-redirects" alt="npm version</a>"></p>
<p><a href="upload://miOrWterVShmDN2zS0noAMRNVMK.svg"><img src="https://github.com/follow-redirects/follow-redirects/actions" alt="Build Status</a>"></p>
<p><a href="upload://2jMWsjNzhhwh1y6tmIrND4jaFIb.svg"><img src="https://coveralls.io/r/follow-redirects/follow-redirects?branch=master" alt="Coverage Status</a>"></p>
<p><a href="upload://ollu8Sa2Rqd3epnC3OfdVUnfDiX.svg"><img src="https://www.npmjs.com/package/follow-redirects" alt="npm downloads</a>"></p>
<p><a href="upload://xcstV1vVc603T7KBtv0K3dzfC3v.svg"><img src="https://github.com/sponsors/RubenVerborgh" alt="Sponsor on GitHub</a>"></p><p><code>follow-redirects</code> provides <a href="https://nodejs.org/api/http.html#http_http_request_options_callback">request</a> and <a href="https://nodejs.org/api/http.html#http_http_get_options_callback">get</a></p>
<p> methods that behave identically to those found on the native <a href="https://nodejs.org/api/http.html#http_http_request_options_callback">http</a> and <a href="https://nodejs.org/api/https.html#https_https_request_options_callback">https</a></p>
<p> modules, with the exception that they will seamlessly follow redirects.</p><pre><code>
const { http, https } = require('follow-redirects');http.get('http://bit.ly/900913', response => {
response.on('data', chunk => {
console.log(chunk);
});
}).on('error', err => {
console.error(err);
});
</code></pre><p>You can inspect the final redirected URL through the <code>responseUrl</code> property on the <code>response</code>.</p>
<p>If no redirection happened, <code>responseUrl</code> is the original request URL.</p><pre><code>
const request = https.request({
host: 'bitly.com',
path: '/UHfDGO',
}, response => {
console.log(response.responseUrl);
// 'http://duckduckgo.com/robots.txt'
});
request.end();
</code></pre><h3>Options</h3>
<h4>Global options</h4>
<p>Global options are set directly on the <code>follow-redirects</code> module:</p><pre><code>
const followRedirects = require('follow-redirects');
followRedirects.maxRedirects = 10;
followRedirects.maxBodyLength = 20 * 1024 * 1024; // 20 MB
</code></pre><p>The following global options are supported:</p>
<ul>
<li><code>maxRedirects</code> (default: <code>21</code>) – sets the maximum number of allowed redirects; if exceeded, an error will be emitted.</li>
</ul><ul>
<li><code>maxBodyLength</code> (default: 10MB) – sets the maximum size of the request body; if exceeded, an error will be emitted.</li>
</ul><h4>Per-request options</h4>
<p>Per-request options are set by passing an <code>options</code> object:</p><pre><code>
const url = require('url');
const { http, https } = require('follow-redirects');const options = url.parse('http://bit.ly/900913');
options.maxRedirects = 10;
options.beforeRedirect = (options, response, request) => {
// Use this to adjust the request options upon redirecting,
// to inspect the latest response headers,
// or to cancel the request by throwing an error// response.headers = the redirect response headers
// response.statusCode = the redirect response code (eg. 301, 307, etc.)// request.url = the requested URL that resulted in a redirect
// request.headers = the headers in the request that resulted in a redirect
// request.method = the method of the request that resulted in a redirect
if (options.hostname === "example.com") {
options.auth = "user:password";
}
};
http.request(options);
</code></pre><p>In addition to the <a href="https://nodejs.org/api/http.html#http_http_request_options_callback">standard HTTP</a> and <a href="https://nodejs.org/api/https.html#https_https_request_options_callback">HTTPS options</a>,</p>
<p>the following per-request option</p><hr>
<p>这个参数你们一般设多少?评论区说说。</p>