cloudflare--workers-sdk
70bf21e064
Prerelease / build (push) Has been skipped
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
47 行
1.7 KiB
JavaScript
47 行
1.7 KiB
JavaScript
/**
|
|
* Welcome to Cloudflare Workers! This is your first worker.
|
|
*
|
|
* - Run `npm run dev` in your terminal to start a development server
|
|
* - Open a browser tab at http://localhost:8787/ to see your worker in action
|
|
* - Run `npm run deploy` to publish your worker
|
|
*
|
|
* Learn more at https://developers.cloudflare.com/workers/
|
|
*/
|
|
|
|
import handleProxy from './proxy';
|
|
import handleRedirect from './redirect';
|
|
import apiRouter from './router';
|
|
|
|
// Export a default object containing event handlers
|
|
export default {
|
|
// The fetch handler is invoked when this worker receives a HTTP(S) request
|
|
// and should return a Response (optionally wrapped in a Promise)
|
|
async fetch(request, env, ctx) {
|
|
// You'll find it helpful to parse the request.url string into a URL object. Learn more at https://developer.mozilla.org/en-US/docs/Web/API/URL
|
|
const url = new URL(request.url);
|
|
|
|
// You can get pretty far with simple logic like if/switch-statements
|
|
switch (url.pathname) {
|
|
case '/redirect':
|
|
return handleRedirect.fetch(request, env, ctx);
|
|
|
|
case '/proxy':
|
|
return handleProxy.fetch(request, env, ctx);
|
|
}
|
|
|
|
if (url.pathname.startsWith('/api/')) {
|
|
// You can also use more robust routing
|
|
return apiRouter.handle(request);
|
|
}
|
|
|
|
return new Response(
|
|
`Try making requests to:
|
|
<ul>
|
|
<li><code><a href="/redirect?redirectUrl=https://example.com/">/redirect?redirectUrl=https://example.com/</a></code>,</li>
|
|
<li><code><a href="/proxy?modify&proxyUrl=https://example.com/">/proxy?modify&proxyUrl=https://example.com/</a></code>, or</li>
|
|
<li><code><a href="/api/todos">/api/todos</a></code></li>`,
|
|
{ headers: { 'Content-Type': 'text/html' } }
|
|
);
|
|
},
|
|
};
|