admin / scripts /deploy /cloudflare-worker-api.js
hanbinsite's picture
chore: HuggingFace deploy 2026-05-19 10:37
dc649b1
Raw History Blame Contribute Delete
962 Bytes
// Cloudflare Worker - API 反向代理
// 目标:bin-api.hanbin666.top → binhugg2-api.hf.space
// 部署方式:Cloudflare Dashboard → Workers & Pages → Create Worker → 粘贴此代码
// 路由绑定:bin-api.hanbin666.top/*
export default {
async fetch(request) {
const url = new URL(request.url);
// 替换目标域名
url.hostname = "binhugg2-api.hf.space";
url.port = "443";
url.protocol = "https:";
const newRequest = new Request(url.toString(), {
method: request.method,
headers: request.headers,
body: ["GET", "HEAD"].includes(request.method) ? undefined : request.body,
redirect: "follow",
});
const response = await fetch(newRequest);
// 透传响应,保留原始 headers
const newResponse = new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
});
return newResponse;
},
};