HBForge/Examples
02 · Applications

Reverse proxy to an upstream service

Forward every request under /api/* to another origin, preserving method, headers, query string and body. Streams back the upstream Response directly.

forge/serverNodeBunDenoCF Workers
Code
proxy.js
const { WebApp } = require(class="tk-str">'@hyperbridge/forge/server');
const app = new WebApp();

app.all(class="tk-str">'/api/*', async (c) => {
  const url = new URL(c.req.url);
  const upstream = class="tk-str">'https:class="tk-cm">//api.example.com'
    + url.pathname.replace(class="tk-str">'/api', class="tk-str">'')
    + url.search;
  return fetch(upstream, {
    method:  c.req.method,
    headers: c.req.header(),
    body:    [class="tk-str">'GET',class="tk-str">'HEAD'].includes(c.req.method)
               ? undefined
               : await c.req.arrayBuffer(),
  });
});

app.listen(3000);
How it works

Returning the upstream Response directly is zero-copy — the body never buffers in your process. WebApp's runtime passes the body stream straight to the client.

c.req.header() returns a plain object of every header, which fetch() accepts as its headers option. c.req.arrayBuffer() is cached, so calling other body methods later won't throw.

On Cloudflare Workers, this becomes a true edge proxy: requests hop into your Worker, are forwarded, and stream back without crossing your origin again.

Try it
Quickstart
curl http://localhost:3000/api/users → forwarded to api.example.com/users
Related modules