x-dr / chatgptProxyAPI

🔥 使用cloudflare 搭建免费的 OpenAI api代理 ,解决网络无法访问问题。支持流式输出
https://chatai.451024.xyz
MIT License
2.95k stars 678 forks source link

搭建好了之后如何修改首页的默认页面? #40

Closed NianBroken closed 1 year ago

NianBroken commented 1 year ago

使用了方法2的CloudFlare Pages进行中转,搭建成功后,访问域名会进入下图所示的页面: image 如何我想自定义页面,应该怎么操作?

x-dr commented 1 year ago

例子


const TELEGRAPH_URL = 'https://api.openai.com';
const page = `
<!DOCTYPE html>
<html>
<head>
    <title>xxx</title>
</head>

<body>
xxx
</body>
</html>

`

export default {
    async fetch(request, env) {
        const NewResponse = await handleRequest(request)
        return NewResponse
    },

};

async function handleRequest(request) {
    const url = new URL(request.url);
    //  路径
    if (url.pathname == "/") {
        return new Response(page, {
            headers: {
                "content-type": "text/html;charset=UTF-8",
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Credentials": "true",
                "Access-Control-Allow-Headers": "*",
                "Access-Control-Allow-Methods": "*",
            },
        })
    }
    else {
        const headers_Origin = request.headers.get("Access-Control-Allow-Origin") || "*"
        url.host = TELEGRAPH_URL.replace(/^https?:\/\//, '');
        const modifiedRequest = new Request(url.toString(), {
            headers: request.headers,
            method: request.method,
            body: request.body,
            redirect: 'follow'
        });
        const response = await fetch(modifiedRequest);
        const modifiedResponse = new Response(response.body, response);
        // 添加允许跨域访问的响应头
        modifiedResponse.headers.set('Access-Control-Allow-Origin', headers_Origin);
        return modifiedResponse;
    }

}