@@ -74,32 +74,42 @@ To override the agent on a per-request basis, use the `agent` option for `http.r
7474const https = require (' node:https' );
7575
7676// Creating a custom agent with custom proxy support.
77- const agent = new https.Agent ({ proxyEnv: { HTTPS_PROXY : ' http://proxy.company.com:8080' } });
78-
79- https .request ({
80- hostname: ' www.external.com' ,
81- port: 443 ,
82- path: ' /' ,
83- agent,
84- }, (res ) => {
85- // This request will be proxied through proxy.company.com:8080 using the HTTP protocol.
77+ const agent = new https.Agent ({
78+ proxyEnv: { HTTPS_PROXY : ' http://proxy.company.com:8080' },
8679});
80+
81+ https .request (
82+ {
83+ hostname: ' www.external.com' ,
84+ port: 443 ,
85+ path: ' /' ,
86+ agent,
87+ },
88+ res => {
89+ // This request will be proxied through proxy.company.com:8080 using the HTTP protocol.
90+ }
91+ );
8792```
8893
8994``` mjs
9095import https from ' node:https' ;
9196
9297// Creating a custom agent with custom proxy support.
93- const agent = new https.Agent ({ proxyEnv: { HTTPS_PROXY : ' http://proxy.company.com:8080' } });
94-
95- https .request ({
96- hostname: ' www.external.com' ,
97- port: 443 ,
98- path: ' /' ,
99- agent,
100- }, (res ) => {
101- // This request will be proxied through proxy.company.com:8080 using the HTTP protocol.
98+ const agent = new https.Agent ({
99+ proxyEnv: { HTTPS_PROXY : ' http://proxy.company.com:8080' },
102100});
101+
102+ https .request (
103+ {
104+ hostname: ' www.external.com' ,
105+ port: 443 ,
106+ path: ' /' ,
107+ agent,
108+ },
109+ res => {
110+ // This request will be proxied through proxy.company.com:8080 using the HTTP protocol.
111+ }
112+ );
103113```
104114
105115To override the agent globally, reset ` http.globalAgent ` and ` https.globalAgent ` :
@@ -112,24 +122,40 @@ To override the agent globally, reset `http.globalAgent` and `https.globalAgent`
112122const http = require (' node:http' );
113123const https = require (' node:https' );
114124
115- http .globalAgent = new http.Agent ({ proxyEnv: { HTTP_PROXY : ' http://proxy.company.com:8080' } });
116- https .globalAgent = new https.Agent ({ proxyEnv: { HTTPS_PROXY : ' http://proxy.company.com:8080' } });
125+ http .globalAgent = new http.Agent ({
126+ proxyEnv: { HTTP_PROXY : ' http://proxy.company.com:8080' },
127+ });
128+ https .globalAgent = new https.Agent ({
129+ proxyEnv: { HTTPS_PROXY : ' http://proxy.company.com:8080' },
130+ });
117131
118132// Subsequent requests will all use the configured proxies, unless they override the agent option.
119- http .request (' http://external.com' , (res ) => { /* ... */ });
120- https .request (' https://external.com' , (res ) => { /* ... */ });
133+ http .request (' http://external.com' , res => {
134+ /* ... */
135+ });
136+ https .request (' https://external.com' , res => {
137+ /* ... */
138+ });
121139```
122140
123141``` mjs
124142import http from ' node:http' ;
125143import https from ' node:https' ;
126144
127- http .globalAgent = new http.Agent ({ proxyEnv: { HTTP_PROXY : ' http://proxy.company.com:8080' } });
128- https .globalAgent = new https.Agent ({ proxyEnv: { HTTPS_PROXY : ' http://proxy.company.com:8080' } });
145+ http .globalAgent = new http.Agent ({
146+ proxyEnv: { HTTP_PROXY : ' http://proxy.company.com:8080' },
147+ });
148+ https .globalAgent = new https.Agent ({
149+ proxyEnv: { HTTPS_PROXY : ' http://proxy.company.com:8080' },
150+ });
129151
130152// Subsequent requests will all use the configured proxies, unless they override the agent option.
131- http .request (' http://external.com' , (res ) => { /* ... */ });
132- https .request (' https://external.com' , (res ) => { /* ... */ });
153+ http .request (' http://external.com' , res => {
154+ /* ... */
155+ });
156+ https .request (' https://external.com' , res => {
157+ /* ... */
158+ });
133159```
134160
135161### Using Proxies with Authentication
@@ -146,13 +172,13 @@ export HTTPS_PROXY=http://username:password@proxy.company.com:8080
146172
147173The ` NO_PROXY ` variable supports:
148174
149- * ` * ` - Bypass proxy for all hosts
150- * ` company.com ` - Exact host name match
151- * ` .company.com ` - Domain suffix match (matches ` sub.company.com ` )
152- * ` *.company.com ` - Wildcard domain match
153- * ` 192.168.1.100 ` - Exact IP address match
154- * ` 192.168.1.1-192.168.1.100 ` - IP address range
155- * ` company.com:8080 ` - Hostname with specific port
175+ - ` * ` - Bypass proxy for all hosts
176+ - ` company.com ` - Exact host name match
177+ - ` .company.com ` - Domain suffix match (matches ` sub.company.com ` )
178+ - ` *.company.com ` - Wildcard domain match
179+ - ` 192.168.1.100 ` - Exact IP address match
180+ - ` 192.168.1.1-192.168.1.100 ` - IP address range
181+ - ` company.com:8080 ` - Hostname with specific port
156182
157183If a target matches ` NO_PROXY ` , the request bypasses the proxy.
158184
@@ -218,8 +244,12 @@ const systemCerts = tls.getCACertificates('system');
218244tls .setDefaultCACertificates ([... currentCerts, ... systemCerts]);
219245
220246// Subsequent requests use system certificates during verification.
221- https .get (' https://internal.company.com' , (res ) => { /* ... */ });
222- fetch (' https://internal.company.com' ).then ((res ) => { /* ... */ });
247+ https .get (' https://internal.company.com' , res => {
248+ /* ... */
249+ });
250+ fetch (' https://internal.company.com' ).then (res => {
251+ /* ... */
252+ });
223253```
224254
225255``` mjs
@@ -230,8 +260,12 @@ const systemCerts = tls.getCACertificates('system');
230260tls .setDefaultCACertificates ([... currentCerts, ... systemCerts]);
231261
232262// Subsequent requests use system certificates during verification.
233- https .get (' https://internal.company.com' , (res ) => { /* ... */ });
234- fetch (' https://internal.company.com' ).then ((res ) => { /* ... */ });
263+ https .get (' https://internal.company.com' , res => {
264+ /* ... */
265+ });
266+ fetch (' https://internal.company.com' ).then (res => {
267+ /* ... */
268+ });
235269```
236270
237271#### Configure CA Certificates for Individual Requests
@@ -241,25 +275,35 @@ To override CA certificates per request, use the `ca` option. This is currently
241275``` cjs
242276const https = require (' node:https' );
243277const specialCerts = [' -----BEGIN CERTIFICATE-----\n ...' ];
244- https .get ({
245- hostname: ' internal.company.com' ,
246- port: 443 ,
247- path: ' /' ,
248- method: ' GET' ,
249- // The `ca` option replaces defaults; concatenate bundled certs if needed.
250- ca: specialCerts,
251- }, (res ) => { /* ... */ });
278+ https .get (
279+ {
280+ hostname: ' internal.company.com' ,
281+ port: 443 ,
282+ path: ' /' ,
283+ method: ' GET' ,
284+ // The `ca` option replaces defaults; concatenate bundled certs if needed.
285+ ca: specialCerts,
286+ },
287+ res => {
288+ /* ... */
289+ }
290+ );
252291```
253292
254293``` mjs
255294import https from ' node:https' ;
256295const specialCerts = [' -----BEGIN CERTIFICATE-----\n ...' ];
257- https .get ({
258- hostname: ' internal.company.com' ,
259- port: 443 ,
260- path: ' /' ,
261- method: ' GET' ,
262- // The `ca` option replaces defaults; concatenate bundled certs if needed.
263- ca: specialCerts,
264- }, (res ) => { /* ... */ });
296+ https .get (
297+ {
298+ hostname: ' internal.company.com' ,
299+ port: 443 ,
300+ path: ' /' ,
301+ method: ' GET' ,
302+ // The `ca` option replaces defaults; concatenate bundled certs if needed.
303+ ca: specialCerts,
304+ },
305+ res => {
306+ /* ... */
307+ }
308+ );
265309```
0 commit comments