node/test/fixtures/fetch-and-log.mjs
Joyee Cheung c0108bfc55
http: add http.setGlobalProxyFromEnv()
This adds an API to dynamically enable built-in proxy support
for all of fetch() and http.request()/https.request(), so
that users do not have to be aware of them all and configure them
one by one.

PR-URL: https://github.com/nodejs/node/pull/60953
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
2025-12-10 16:12:28 +00:00

20 lines
569 B
JavaScript

import http from 'node:http';
let restore;
if (process.env.SET_GLOBAL_PROXY) {
const config = JSON.parse(process.env.SET_GLOBAL_PROXY);
restore = http.setGlobalProxyFromEnv(config);
} else if (process.env.SET_GLOBAL_PROXY_DEFAULT) {
restore = http.setGlobalProxyFromEnv();
}
const response = await fetch(process.env.FETCH_URL);
const body = await response.text();
console.log(body);
if (process.env.CLEAR_GLOBAL_PROXY_AND_RETRY) {
restore();
const response = await fetch(process.env.FETCH_URL);
const body = await response.text();
console.log(body);
}