Skip to content

Instantly share code, notes, and snippets.

@itzzzme
Last active April 20, 2025 11:13
Show Gist options
  • Save itzzzme/180813be2c7b45eedc8ce8344c8dea3b to your computer and use it in GitHub Desktop.
Save itzzzme/180813be2c7b45eedc8ce8344c8dea3b to your computer and use it in GitHub Desktop.
Setting up reversed proxy server
//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>
@LeetIDA
Copy link

LeetIDA commented Jan 18, 2025

✘ [ERROR] Build failed with 2 errors:

✘ [ERROR] Could not resolve "hono"

  src/index.js:5:21:
    5 │ import { Hono } from 'hono';
      ╵                      ~~~~~~

You can mark the path "hono" as external to exclude it from the bundle, which will remove this

error.

✘ [ERROR] Could not resolve "hono/cors"

  src/index.js:6:21:
    6 │ import { cors } from 'hono/cors';
      ╵                      ~~~~~~~~~~~

You can mark the path "hono/cors" as external to exclude it from the bundle, which will remove

this error.

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