Last active
October 28, 2023 06:14
-
-
Save so298/b39913536ea6ad51c87a74353967eabd to your computer and use it in GitHub Desktop.
deno proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a reverse proxy example written in TypeScript that runs on Deno.