md/example/worker.js
Honwhy Wang d37aedb95d
All checks were successful
Build and Deploy / build-and-deploy (push) Has been skipped
feat: use mp oss in website (#471)
* add example cloudflare worker proxy
* use wsrv.nl to host image request in website
2024-12-14 16:14:33 +08:00

37 lines
1011 B
JavaScript

/**
* @typedef {object} Env
* @property
*/
export default {
/**
* @param {Request} request
* @param {Env} env
* @param {ExecutionContext} ctx
* @returns {Promise<Response>}
*/
async fetch(request, env, ctx) {
const url = new URL(request.url)
const targetUrl = `https://api.weixin.qq.com`
const proxyRequest = new Request(targetUrl + url.pathname + url.search, {
method: request.method,
headers: request.headers,
body: request.body,
})
const response = await fetch(proxyRequest)
const proxyResponse = new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
})
setCorsHeaders(proxyResponse.headers)
return proxyResponse
},
}
// 设置 CORS 头部
function setCorsHeaders(headers) {
headers.set(`Access-Control-Allow-Origin`, `*`)
headers.set(`Access-Control-Allow-Methods`, `GET, POST, PUT, DELETE`)
headers.set(`Access-Control-Allow-Headers`, `*`)
}