cloudflare如何使用worker搭建gemini api的代理服务?
网友回复
新建一个worker,然后worker代码如下:
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request));
})
async function fetchAndApply(request) {
if (request.method === 'OPTIONS') {
// 对于预检请求,设置允许的请求方法、请求头和响应头
return new Response(null, {
headers: {
'Access-Control-Allow-Origin': '*', // 允许的域名,可以设置具体的域名
'Access-Control-Allow-Methods': '*', // 允许的请求方法
'Access-Control-Allow-Headers': '*' // 允许的请求头,例如 Content-Type 和 Authorization
}
})
}
let response = null;
let method = request.method;
let request_headers = request.headers;
...点击查看剩余70%


