diff --git a/README.md b/README.md index fa5fc68..d0273d9 100644 --- a/README.md +++ b/README.md @@ -126,9 +126,10 @@ Settings > Functions > Advanced Setting > Function Region 切换为 Hong Kong, ## 部署到 腾讯云 edgeone pages 【推荐】 ### 一键部署 -[![使用 EdgeOne Pages 部署](https://cdnstatic.tencentcs.com/edgeone/pages/deploy.svg)](https://edgeone.ai/pages/new?template=https://github.com/huangxd-/danmu_api&install-command=npm%20install&start-command=npm%20start&root-directory=.%2F&env=TOKEN) +[![使用 EdgeOne Pages 部署](https://cdnstatic.tencentcs.com/edgeone/pages/deploy.svg)](https://console.cloud.tencent.com/edgeone/pages/new?template=https://github.com/huangxd-/danmu_api&project-name=danmu-api&root-directory=.%2F&env=TOKEN) > 注意:部署时请在环境变量配置区域填写你的TOKEN值,该变量将用于API服务的身份验证相关功能 +> 项目名称不能有下划线 ## 部署到 Cloudflare diff --git a/danmu_api/worker.js b/danmu_api/worker.js index 7ec5f73..9864d9e 100644 --- a/danmu_api/worker.js +++ b/danmu_api/worker.js @@ -1,6 +1,6 @@ // 全局状态(Cloudflare 和 Vercel 都可能重用实例) // ⚠️ 不是持久化存储,每次冷启动会丢失 -const VERSION = "1.0.1"; +const VERSION = "1.0.2"; let animes = []; let episodeIds = []; let episodeNum = 10001; // 全局变量,用于自增 ID diff --git a/edgeone.json b/edgeone.json index 9878e66..da4a5d2 100644 --- a/edgeone.json +++ b/edgeone.json @@ -1,25 +1,14 @@ { - "version": 1, - "routes": [ + "name": "danmu-api", + "outputDirectory": "./", + "rewrites": [ { - "pattern": "/", - "function": "index" + "source": "/", + "destination": "/node-functions/index.js" }, { - "pattern": "/api/*", - "function": "index" - }, - { - "pattern": "/*/api/*", - "function": "index" + "source": "/*", + "destination": "/node-functions/[[...path]].js" } - ], - "functions": { - "index": { - "src": "danmu_api/worker.js" - } - }, - "env": { - "TOKEN": "87654321" - } + ] } \ No newline at end of file diff --git a/node-functions/[[...path]]..js b/node-functions/[[...path]]..js new file mode 100644 index 0000000..e9a0613 --- /dev/null +++ b/node-functions/[[...path]]..js @@ -0,0 +1,3 @@ +import { onRequest } from './index.js'; // 改为 onRequest,支持所有方法 + +export { onRequest }; // 复用 index.js 的 onRequest \ No newline at end of file diff --git a/node-functions/index.js b/node-functions/index.js new file mode 100644 index 0000000..76f8d3d --- /dev/null +++ b/node-functions/index.js @@ -0,0 +1,36 @@ +import { handleRequest } from '../danmu_api/worker.js'; + +export const onRequest = async (context) => { + const { request, env } = context; + + // 获取协议和主机名,使用属性访问而非 get 方法 + const baseUrl = `https://localhost`; + + // 调试:打印 headers 和原始 URL + console.log('Request URL:', request.url); + console.log('Request Headers:', request.headers); + + // 构造完整的 URL + let fullUrl; + try { + fullUrl = new URL(request.url, baseUrl).toString(); + console.log('Request fullUrl:', fullUrl); + } catch (error) { + console.error('URL Construction Error:', error); + return new Response('Invalid URL', { status: 400 }); + } + + // 创建新的 request 对象,替换 url + const modifiedRequest = new Request(fullUrl, { + method: request.method, + headers: request.headers, + body: request.body, + redirect: request.redirect, + credentials: request.credentials, + cache: request.cache, + mode: request.mode + }); + + // 传递修改后的 request 和 env 给 handleRequest + return await handleRequest(modifiedRequest, env); +}; \ No newline at end of file