Last active
October 29, 2022 13:39
-
-
Save Lighfer/fc27dd3b9a9d933e5f26fd6a722d6f7b to your computer and use it in GitHub Desktop.
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
| import { serve } from "https://deno.land/std@0.155.0/http/server.ts" | |
| const targetSite = 'https://www.baidu.com/' | |
| serve(async (req: Request) => { | |
| const url = new URL(req.url) | |
| let targetUrl = url.href.replace(`${url.origin}/`, '') | |
| if (!targetUrl.startsWith('http')) { | |
| targetUrl = `${targetSite}${targetUrl}` | |
| } | |
| console.log(`${targetUrl}`) | |
| let urlObj: any | |
| try { | |
| urlObj = new URL(targetUrl) | |
| } catch (e) { | |
| console.error(e.message) | |
| } | |
| if (['http:', 'https:'].indexOf(urlObj?.protocol) > -1) { | |
| let res = await fetch(targetUrl, { | |
| headers: req.headers, | |
| method: req.method, | |
| body: req.body, | |
| }) | |
| let headers = {} | |
| res.headers.forEach((value, key) => { | |
| headers[key] = value | |
| }) | |
| if ('*' !== headers['Access-Control-Allow-Origin']?.trim() | |
| && '*' !== headers['access-control-allow-origin']?.trim()) { | |
| headers['Access-Control-Allow-Origin'] = '*' | |
| } | |
| return new Response(res.body, { headers, status: res.status }) | |
| } | |
| return new Response( | |
| `Usage: ${url.origin}/https://deno.com/deploy/docs/pricing-and-limits`) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment