Cloudflare Workers runs JavaScript at the edge — 300+ data centres worldwide with sub-1ms cold starts.
Install and Create Project
npm install -g wrangler
wrangler auth login
wrangler init my-worker && cd my-worker
Worker Code
export default {
async fetch(request, env) {
const url = new URL(request.url);
if (url.pathname.startsWith('/api/')) {
return fetch('https://api.yourdomain.com' + url.pathname, {
headers: { 'Authorization': 'Bearer ' + env.API_KEY }
});
}
return fetch(request);
}
};
Deploy
wrangler secret put API_KEY
wrangler deploy
Conclusion
Cloudflare Workers reduces origin server load globally. Our team implements edge computing solutions using Workers.
Comments