-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add example cloudflare worker proxy * use wsrv.nl to host image request in website
- Loading branch information
Showing
7 changed files
with
86 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Example | ||
|
||
## worker.js | ||
|
||
公众号openapi接口代理服务示例,该项目将请求转发至微信公众号api。 | ||
|
||
开发调试: | ||
|
||
``` | ||
cd example | ||
npx wrangler dev worker.js | ||
``` | ||
|
||
部署: | ||
|
||
请将其部署到cloudflare workers。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @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`, `*`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters