Last active
November 28, 2025 06:12
-
-
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> |
Hm
✘ [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 thiserror.
✘ [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 removethis error.
run "npm install" before you "wrangler deploy" or use below worker.js:
export default {
async fetch(request, env, ctx) {
if (request.method === 'OPTIONS') {
return handleOptions(request);
}
const url = new URL(request.url);
const targetUrl = url.searchParams.get('url');
if (!targetUrl) {
return new Response('Missing target URL', { status: 400 });
}
try {
const target = new URL(targetUrl);
const headers = new Headers();
for (const [key, value] of request.headers) {
if (!['origin', 'referer'].includes(key.toLowerCase())) {
headers.set(key, value);
}
}
const response = await fetch(target, {
method: request.method,
headers: headers,
body: request.method === 'GET' || request.method === 'HEAD' ? null : request.body,
});
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, *',
'Access-Control-Max-Age': '600',
};
return new Response(response.body, {
status: response.status,
headers: {
...Object.fromEntries(response.headers),
...corsHeaders,
},
});
} catch (error) {
return new Response('Invalid URL', { status: 400 });
}
},
};
function handleOptions(request) {
return new Response(null, {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, *',
'Access-Control-Max-Age': '600',
},
});
}
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.