问题
In the node https module docs, regarding https.request, an example is shown:
const options = {
hostname: 'encrypted.google.com',
port: 443,
path: '/',
method: 'GET',
key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
options.agent = new https.Agent(options);
const req = https.request(options, (res) => {
// ...
});
What is confusing here to me is the that the request options are passed to the Agent object as well - which is then assigned to the agent property of the options object, to replace the default globalAgent object.
The Agent object does accept tls connection options -
interface AgentOptions extends http.AgentOptions, tls.ConnectionOptions
These do include "key" and "cert" among other things.
What I don't understand is why pass these tls options to both the request method and the Agent constructor - theoretically configuring the Agent object should be enough for the request to use tls, or am I misunderstanding what the agent does? Honestly I've read through some docs pages and it still isn't clear to me if the Agent just manages connection pools or if it also basically is "in charge" of making and configuring the actual requests.
来源:https://stackoverflow.com/questions/64076241/why-does-node-https-module-documentation-example-pass-tls-options-to-both-the-ag