Why does node https module documentation example pass tls options to both the agent and the request options?

不羁岁月 提交于 2021-01-29 07:18:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!