使用Cloudflare Worker代理后端请求

This commit is contained in:
✨小透明・宸✨
2020-05-26 02:09:30 +08:00
parent 5374a34812
commit 7b3fe22947
4 changed files with 83 additions and 30 deletions

32
cfworker_proxy.js Normal file
View File

@@ -0,0 +1,32 @@
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
});
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
let responseConfig = {
status: 200,
headers: {
'Access-Control-Allow-Origin': 'https://akarin.dev',
'Content-Type': 'application/json',
},
}
let articleURL = new URL(request.url).searchParams.get('url');
if (!articleURL || !articleURL.startsWith('https://mp.weixin.qq.com')) {
return new Response('{"success":false}', responseConfig);
}
let requestURL = new URL('https://i.akarin.dev/misc/get_article_info.php');
requestURL.searchParams.set('url', articleURL);
return new Response(
await (
await fetch(requestURL.toString())
).text(),
responseConfig
);
}