Skip to content

Instantly share code, notes, and snippets.

@real-jiakai
Created March 6, 2024 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save real-jiakai/b741342f12d21d25ec705422306a87a8 to your computer and use it in GitHub Desktop.
Save real-jiakai/b741342f12d21d25ec705422306a87a8 to your computer and use it in GitHub Desktop.
反代github api
export default {
async fetch(request, env, ctx) {
try {
// 用您要代理的 GitHub API URL 替换这个 URL
const url = new URL('https://api.github.com' + new URL(request.url).pathname)
// 克隆原始请求,但更改其 URL
const newRequest = new Request(url, {
body: request.body,
headers: request.headers,
method: request.method
})
// 转发请求到 GitHub API
const response = await fetch(newRequest)
// 克隆响应对象,以便可以修改头部
const newResponse = new Response(response.body, response)
// 允许跨域请求
newResponse.headers.set('Access-Control-Allow-Origin', '*')
// 返回新的响应对象
return newResponse
} catch (e) {
// 如果有错误,返回错误信息
return new Response(e.message || 'An error occurred', { status: 500 })
}
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment