# Use a proxy with Node.js

Complete proxy requests using undici ProxyAgent, environment variables, AbortSignal timeouts, and security logs.

Node.js example using caller project locked `undici` Dependency. The account is closed. `us.zzhttp.com:7878` Username and password are read from the controlled environment.

## Node.js

```javascript
import { ProxyAgent, fetch } from 'undici';

const username = process.env.PROXY_USERNAME;
const password = process.env.PROXY_PASSWORD;
if (!username || !password) throw new Error('Missing proxy credentials');
const proxyUrl = new URL('http://us.zzhttp.com:7878');
proxyUrl.username = username;
proxyUrl.password = password;
const dispatcher = new ProxyAgent(proxyUrl.toString());
try {
  const response = await fetch('https://ipinfo.io/json', {
    dispatcher,
    signal: AbortSignal.timeout(30_000),
  });
  if (!response.ok) throw new Error(`HTTP ${response.status}`);
  console.log(await response.json());
} finally {
  await dispatcher.close();
}
```

## Configuration

- Read the full proxy username and sub-username and password from the controlled environment variable, and do not output the spelled proxy URL to the console.

## Request

- - Will. `ProxyAgent` As a fetch dispatcher.
- Use `AbortSignal.timeout` Set the overall deadline.

## Resource release

- After the example is finished, close the agent.
- Production connection pools are reused as agents and deprecated when configuration changes.

## Error handling

- Distinguish between timeouts, proxy authentication, and target status.
- The log records only the task ID and status.

html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}
