1. Home
  2. /
  3. Cloudflare-workers
  4. /
  5. Cloudflare Worker with Environment Variable

Cloudflare Worker with Environment Variable - Cloudflare-workers Typing CST Test

Loading…

Cloudflare Worker with Environment Variable — Cloudflare-workers Code

A Worker using environment variables from Cloudflare KV or Secrets.

# cloudflare/demo/env.js
addEventListener('fetch', event => {
	event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
	const apiKey = MY_SECRET_API_KEY
	return new Response('API Key length: ' + apiKey.length, { status: 200 })
}

Cloudflare-workers Language Guide

Cloudflare Workers is a serverless platform for deploying JavaScript, TypeScript, and Rust functions at the edge, allowing developers to run code globally close to end-users with low latency.

Primary Use Cases

  • ▸Edge APIs and middleware
  • ▸Request/response manipulation for websites
  • ▸Bot and security logic at the edge
  • ▸Global serverless functions and microservices
  • ▸Integrating with KV storage or Durable Objects

Notable Features

  • ▸Runs code at the edge globally
  • ▸Automatic scaling with no server management
  • ▸Supports JavaScript, TypeScript, Rust, WebAssembly
  • ▸Integrates with Cloudflare KV, Durable Objects, and R2
  • ▸Fast execution and low-latency responses

Origin & Creator

Cloudflare Workers was launched by Cloudflare in 2017 as a way to run serverless code at the edge.

Industrial Note

Cloudflare Workers excels in global, low-latency applications where edge execution and near-instant scalability are critical.

Quick Explain

  • ▸Workers execute code on Cloudflare’s edge network, reducing latency by running near users.
  • ▸Supports multiple languages via WebAssembly (Wasm) including JavaScript, TypeScript, Rust, and C/C++.
  • ▸Designed for serverless applications without managing servers or infrastructure.
  • ▸Integrates with Cloudflare’s services like KV storage, Durable Objects, and R2 storage.
  • ▸Ideal for building APIs, middleware, bots, and high-performance edge logic.

Core Features

  • ▸Function-as-a-Service (FaaS) model at edge locations
  • ▸HTTP request/response handling
  • ▸KV storage for serverless state
  • ▸Durable Objects for consistent state across requests
  • ▸Routing via Cloudflare Workers Routes and Workers Sites

Learning Path

  • ▸Learn JavaScript/TypeScript basics
  • ▸Understand edge computing concepts
  • ▸Install Wrangler CLI and deploy first worker
  • ▸Integrate KV and Durable Objects
  • ▸Monitor and scale edge functions globally

Practical Examples

  • ▸Edge caching and request rewriting
  • ▸Global API endpoint
  • ▸Bot detection and security logic
  • ▸Serverless image optimization
  • ▸Real-time analytics at the edge

Comparisons

  • ▸Cloudflare Workers vs AWS Lambda: Workers run at edge nodes globally, Lambda is region-based
  • ▸Workers vs Fission: Workers run on Cloudflare edge, Fission runs on Kubernetes
  • ▸Workers vs Fastly Compute@Edge: Both edge serverless, different providers
  • ▸Workers vs Micronaut: Micronaut is full-stack framework, Workers is FaaS at the edge
  • ▸Workers vs Node.js server: Node.js requires server hosting, Workers run serverless at edge

Strengths

  • ▸Ultra-low latency due to edge execution
  • ▸Scales automatically with traffic
  • ▸Integrates with Cloudflare security and performance tools
  • ▸Supports multiple runtimes and WebAssembly
  • ▸No infrastructure or server maintenance required

Limitations

  • ▸Vendor lock-in to Cloudflare network
  • ▸Limited runtime execution duration (50ms-10s typical)
  • ▸Cold start negligible but complex workflows may require orchestration
  • ▸Limited built-in debugging compared to traditional servers
  • ▸State persistence requires KV, Durable Objects, or external storage

When NOT to Use

  • ▸Applications requiring long-running compute tasks
  • ▸Heavy backend processing not suitable for edge
  • ▸Workflows needing advanced orchestration
  • ▸Stateful applications without KV/Durable Objects
  • ▸Projects not leveraging edge distribution

Cheat Sheet

  • ▸wrangler generate project-name -> create new worker project
  • ▸wrangler dev -> run worker locally
  • ▸wrangler publish -> deploy worker globally
  • ▸wrangler kv:namespace create -> create KV namespace
  • ▸wrangler secret put -> store secrets securely

FAQ

  • ▸Is Cloudflare Workers free?
  • ▸Yes - free tier available with limits; paid tiers expand usage.
  • ▸Does it support multiple languages?
  • ▸Yes - JavaScript, TypeScript, Rust, WebAssembly modules.
  • ▸Can Workers run globally?
  • ▸Yes - deployed across Cloudflare’s edge network.
  • ▸Do Workers scale automatically?
  • ▸Yes - no servers to manage, scales with requests.
  • ▸Can Workers maintain state?
  • ▸Yes - via KV storage or Durable Objects.

30-Day Skill Plan

  • ▸Week 1: JS/TS basics and Wrangler CLI setup
  • ▸Week 2: Simple HTTP request/response workers
  • ▸Week 3: KV storage integration and Durable Objects
  • ▸Week 4: Edge caching, middleware, and routing
  • ▸Week 5: Deployment, monitoring, and optimization

Final Summary

  • ▸Cloudflare Workers is a serverless edge computing platform.
  • ▸Executes JavaScript, TypeScript, Rust, or WebAssembly near users.
  • ▸Supports KV storage and Durable Objects for state management.
  • ▸Automatic global scaling and low-latency responses.
  • ▸Ideal for edge APIs, middleware, bots, and high-performance serverless apps.

Project Structure

  • ▸src/ - source code
  • ▸wrangler.toml - project configuration
  • ▸package.json - dependencies for JS/TS projects
  • ▸dist/ - compiled or built output
  • ▸tests/ - unit and integration tests

Monetization

  • ▸Serverless API backend for SaaS
  • ▸Global edge middleware for websites
  • ▸Real-time analytics and event processing
  • ▸Serverless bots and automation
  • ▸High-performance edge services for clients

Productivity Tips

  • ▸Keep Workers lightweight for speed
  • ▸Leverage KV/Durable Objects for state
  • ▸Use Wrangler CLI for deployment efficiency
  • ▸Monitor edge performance regularly
  • ▸Combine Workers with caching and Cloudflare services

Basic Concepts

  • ▸Worker - deployed serverless function
  • ▸Route - URL pattern that triggers the worker
  • ▸KV - global key-value storage
  • ▸Durable Object - consistent state object across requests
  • ▸Wrangler - CLI tool to build, test, and deploy Workers

Official Docs

  • ▸https://developers.cloudflare.com/workers/
  • ▸https://developers.cloudflare.com/workers/documentation/

More Cloudflare-workers Typing Exercises

Simple Cloudflare Worker (JavaScript)Cloudflare Worker with JSON ResponseCloudflare Worker RedirectCloudflare Worker Echo RequestCloudflare Worker with Query ParametersCloudflare Worker with HTML ResponseCloudflare Worker with Custom HeadersCloudflare Worker Fetch External APICloudflare Worker with Conditional Response

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher