Last active
April 20, 2025 11:13
-
-
Save itzzzme/180813be2c7b45eedc8ce8344c8dea3b to your computer and use it in GitHub Desktop.
Setting up reversed proxy server
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
//install wrangle and init a project to initialize the worker | |
//add the code given below in index.js | |
import { Hono } from 'hono'; | |
import { cors } from 'hono/cors'; | |
const app = new Hono(); | |
app.use( | |
'*', | |
cors({ | |
origin: '*', | |
allowHeaders: '*', | |
allowMethods: ['GET', 'OPTIONS'], | |
maxAge: 600, | |
}) | |
); | |
app.all('*', async (c) => { | |
const targetUrl = c.req.query('url'); | |
if (!targetUrl) { | |
return c.text('Missing target URL', 400); | |
} | |
const url = new URL(targetUrl); | |
const targetRequest = new Request(url, { | |
method: c.req.method, | |
headers: c.req.headers, | |
body: ['GET', 'HEAD'].includes(c.req.method) ? null : c.req.body, | |
}); | |
const response = await fetch(targetRequest); | |
const newHeaders = new Headers(response.headers); | |
newHeaders.set('Access-Control-Allow-Origin', '*'); | |
newHeaders.set('Access-Control-Allow-Methods', '*'); | |
newHeaders.set('Access-Control-Allow-Headers', 'Content-Type'); | |
return new Response(response.body, { | |
status: response.status, | |
headers: newHeaders, | |
}); | |
}); | |
export default app; | |
// add name of your project (can be any) in wrangler.toml file | |
//run npx wrangler publish | |
// your reversed proxy server is now hosted | |
// to use the proxy make request like this structure given below | |
//https://workername.workers.dev/?url=https://<website_name> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
✘ [ERROR] Build failed with 2 errors:
✘ [ERROR] Could not resolve "hono"
error.
✘ [ERROR] Could not resolve "hono/cors"
this error.