§ Install undici:
npm i undici
§ Instantiate an EnvHttpProxyAgent:
const { EnvHttpProxyAgent }
cosnt proxyAgent = new EnvHttpProxyAgent()
This automatically detect proxy environment variables HTTP_PROXY and HTTPS_PROXY. It works both with and without proxy.
§ Define a function fetch which wraps request of undici with the EnvHttpProxyAgent:
const fetch = async (url) => request(url, { dispatcher: proxyAgent })
§ Adjust your codebase since this fetch and builtin fetch have the slightly different return types. But it is not difficult.
§ In addition, if your organization use a self-signed certificate, on runtime:
NODE_EXTRA_CA_CERTS=<CA cert file path> node app.js
Top comments (0)