Closed NianBroken closed 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;
}
}
使用了方法2的
CloudFlare Pages
进行中转,搭建成功后,访问域名会进入下图所示的页面: 如何我想自定义页面,应该怎么操作?