ProxyProtocol

Protocol handler that proxies requests to other servers.

Protocol handler that proxies requests to other servers.

Forwards requests to local development servers for hot-reloading workflows.

View source ↗

Constructor

new ProxyProtocol(resolver: Record<string, string> | ((uri: string) => Promise<string | null>));

Prop

Type

Methods

Prop

Type

Examples

const protocol = new ProxyProtocol({
  myapp: 'http://localhost:3000',
  api: 'http://localhost:8080',
});

// This proxies to http://localhost:3000/index.html
const response = await protocol.handle('get', 'app://myapp/index.html');

constructor

const protocol = new ProxyProtocol({
  myapp: 'http://localhost:3000',
  api: 'http://localhost:8080',
});
// Proxies `app://myapp/index.html` to `http://localhost:3000/index.html`
const protocol = new ProxyProtocol(async uri => {
  const port = await lookupDevServer(new URL(uri).hostname);
  return port != null ? `http://localhost:${port}` : null;
});

handle

// Proxies to http://localhost:3000/api/data?foo=bar
const response = await protocol.handle('get', 'app://myapp/api/data?foo=bar');
console.log(response.status);
// POST with a body
const response = await protocol.handle(
  'post',
  'app://api/submit',
  { 'Content-Type': 'application/json' },
  Buffer.from(JSON.stringify({ hello: 'world' })),
);

On this page