← BackHow to use a proxy in a nodejs environmentu11g.com
12/8/2024

How to use a proxy in a nodejs environment

There is an established standard by which proxies are configured. It runs via the following environment variables:

  • https_proxy: Proxy for https traffic
  • http_proxy: Proxy for http traffic
  • no_proxy: URLs that should not run via a proxy.
https_proxy
http_proxy
no_proxy

The native fetch client of NodeJS does not offer any functionality for this out-of-the-box, but there is an agent from the undici http client that you can use:

fetch
undici
import { EnvHttpProxyAgent } from "undici"; 
const ENV_HTTP_PROXY_AGENT = new EnvHttpProxyAgent(); 
const proxyAgent = { dispatcher: ENV_HTTP_PROXY_AGENT }; 
await fetch("https://...", { ...proxyAgent, });

The node type definition does not support a dispatcher attribute for fetch, but it’s a supported logic. So if you’re using TypeScript you can ignore the error or use the beloved as any pattern for the proxy agent.

const proxyAgent = { dispatcher: ENV_HTTP_PROXY_AGENT } as any;

And that’s everything, no manual evaluation of the environment variables. Everything is handled by the EnvHttpProxyAgent from undici.

EnvHttpProxyAgent
undici
Home
Projects
Newsletter
Blog
Me
Philosophy
Gen Art
Work
Setup
CV
Machine
Dynamic Pong: Territory Conquest Meets Classic Physics
Introducing Camunda Directory
Getting Started with AIME Directory Collections in 5 Minutes
How to use a proxy in a nodejs environment
Introducing weeklyfoo
Create your own epaper calendar with Canvas
Codespaces can become a game changer
Cloudflare pages direct upload with stable preview urls
Building your own Twitter Thread Generator
300 APIs integrated in minutes, not days
Be an orchestration hero
Cut a few braids - new NPM package
ETHme - your chic web3 identity
Automatically update data and commit
Aren't the standard actions going too far for you? Write your own one!
Screenshots - a perfect task to automate!
Send messages to Slack from Camunda Cloud
5 Steps how to track your Team's Mood
Automate your manual tasks with Camunda and Trello!
Go beyond the basics
Is there an alternative to spaghetti?
Play with node-canvas and build something useful
How do I implement a command line tool?
Aime Directory
Boring Dev Tools
Camunda Directory
Data Democracy
Dev Pulse
Dynamic Pong
Lifosy
Art u11g
dapphuntr
DiyPunks
flethy
Maker Oracles
SwagTime
EthME
fcheat
Find the next victim
Generative Arts
MakerDAO Delegates
SkunkWorks NFT
Trees of Life NFT
zukuNFT
RestZeebe
Websiteshot
Zeebetron: How to Manage Multiple Zeebe Profiles with Electron
dot calendar
Sharu
Switch Interface (theme)
LINKEDIN
X
GITHUB