feat: 添加EdgeOne函数示例和更新.gitignore
添加三个EdgeOne函数示例: 1. helloworld-cloud - 基础云函数示例 2. helloworld-edge - 返回JSON的Edge函数示例 3. random-acg - 带错误处理的图片代理函数 更新.gitignore以排除EdgeOne相关文件和目录
This commit is contained in:
32
edge-functions/random-acg/index.js
Normal file
32
edge-functions/random-acg/index.js
Normal file
@@ -0,0 +1,32 @@
|
||||
export async function onRequest(context) {
|
||||
// 回源 URL 改写
|
||||
const url = `https://riscv-nas.acetaffy.top/random?type=horizontal`;
|
||||
|
||||
try {
|
||||
console.log(`[random-acg] Fetching: ${url}`);
|
||||
// fetch(url) 获取 EdgeOne CDN 缓存与回源。
|
||||
const response = await fetch(url, {
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
||||
}
|
||||
});
|
||||
|
||||
// 克隆响应以避免 body used 错误(虽然直接返回通常没问题)
|
||||
// 并保留原始 headers
|
||||
return new Response(response.body, {
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
headers: response.headers
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
console.error(`[random-acg] Error: ${err.message}`);
|
||||
// 简单的错误处理
|
||||
return new Response(JSON.stringify({
|
||||
error: `Error fetching image: ${err.message}`
|
||||
}), {
|
||||
status: 500,
|
||||
headers: { 'content-type': 'application/json' }
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user