Skip to content

Instantly share code, notes, and snippets.

@so298
Last active October 28, 2023 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save so298/b39913536ea6ad51c87a74353967eabd to your computer and use it in GitHub Desktop.
Save so298/b39913536ea6ad51c87a74353967eabd to your computer and use it in GitHub Desktop.
deno proxy
// main.ts
import { serve } from "https://deno.land/std/http/server.ts";
const target = "https://example.com"; // destination URL
const port = 8000;
async function handleRequest(req: Request): Promise<Response> {
const url = new URL(req.url);
const targetUrl = `${target}${url.pathname}${url.search}`;
const response = await fetch(targetUrl, {
method: req.method,
headers: req.headers,
body: req.method === "POST" || req.method === "PUT" ? await req.text() : undefined,
});
const responseBody = await response.arrayBuffer();
const responseHeaders = new Headers(response.headers);
return new Response(responseBody, {
status: response.status,
headers: responseHeaders,
});
}
console.log(`HTTP proxy server running at http://localhost:${port}/`);
serve(handleRequest, { port })
@so298
Copy link
Author

so298 commented Oct 28, 2023

This is a reverse proxy example written in TypeScript that runs on Deno.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment