Convert Figma logo to code with AI

cloudflare logoworkers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®

2,626
692
2,626
581

Top Related Projects

95,926

A modern runtime for JavaScript and TypeScript.

124,777

The React Framework

17,877

A Git-based CMS for Static Site Generators

⚡ Serverless Framework – Effortlessly build apps that auto-scale, incur zero costs when idle, and require minimal maintenance using AWS Lambda and other managed cloud services.

31,844

Fast and low overhead web framework, for Node.js

66,731

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀

Quick Overview

The cloudflare/workers-sdk repository is a comprehensive toolkit for developing, testing, and deploying Cloudflare Workers. It provides a set of tools and utilities to streamline the development process for serverless applications on Cloudflare's edge network.

Pros

  • Unified development experience for Cloudflare Workers
  • Supports multiple programming languages and frameworks
  • Includes local development and testing capabilities
  • Offers seamless integration with Cloudflare's ecosystem

Cons

  • Learning curve for developers new to Cloudflare Workers
  • Limited to Cloudflare's infrastructure and ecosystem
  • May require additional configuration for complex projects
  • Documentation can be overwhelming for beginners

Code Examples

  1. Creating a simple Worker:
addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  return new Response('Hello World!')
}
  1. Using Durable Objects:
export class Counter {
  constructor(state, env) {
    this.state = state;
  }

  async fetch(request) {
    let value = await this.state.storage.get("value") || 0;
    await this.state.storage.put("value", ++value);
    return new Response(value);
  }
}
  1. Handling routing with itty-router:
import { Router } from 'itty-router'

const router = Router()

router.get('/api/users', () => new Response('List of users'))
router.post('/api/users', () => new Response('Create user'))

addEventListener('fetch', event =>
  event.respondWith(router.handle(event.request))
)

Getting Started

  1. Install Wrangler CLI:

    npm install -g @cloudflare/wrangler
    
  2. Create a new Worker project:

    wrangler init my-worker
    cd my-worker
    
  3. Develop and test locally:

    wrangler dev
    
  4. Deploy to Cloudflare:

    wrangler publish
    

Competitor Comparisons

95,926

A modern runtime for JavaScript and TypeScript.

Pros of Deno

  • Broader ecosystem support and compatibility with Node.js modules
  • Built-in TypeScript support without additional configuration
  • More comprehensive standard library with built-in utilities

Cons of Deno

  • Slower adoption rate in production environments
  • Limited compatibility with some existing Node.js packages
  • Steeper learning curve for developers familiar with Node.js

Code Comparison

Deno:

import { serve } from "https://deno.land/std@0.140.0/http/server.ts";

serve((req) => new Response("Hello World!"));

Workers-SDK:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  return new Response("Hello World!")
}

Both Deno and Workers-SDK aim to provide modern JavaScript/TypeScript runtimes for server-side development. Deno focuses on security, modularity, and built-in TypeScript support, while Workers-SDK is tailored for Cloudflare's edge computing platform. Deno offers a more comprehensive standard library and broader ecosystem compatibility, but Workers-SDK provides seamless integration with Cloudflare's infrastructure and services. The choice between the two depends on specific project requirements and deployment preferences.

124,777

The React Framework

Pros of Next.js

  • Rich ecosystem with extensive documentation and community support
  • Built-in server-side rendering and static site generation capabilities
  • Seamless integration with React and a wide range of third-party libraries

Cons of Next.js

  • Steeper learning curve for developers new to React or server-side rendering
  • Potentially larger bundle sizes compared to more lightweight alternatives
  • Less flexibility in deployment options, primarily optimized for Vercel hosting

Code Comparison

Next.js:

// pages/index.js
export default function Home() {
  return <h1>Welcome to Next.js!</h1>
}

Workers SDK:

// index.js
addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  return new Response('Welcome to Cloudflare Workers!', {
    headers: { 'content-type': 'text/plain' },
  })
}

The Next.js example demonstrates a simple React component for a home page, while the Workers SDK example shows a basic worker script handling HTTP requests. Next.js focuses on React-based web applications, whereas Workers SDK provides a more low-level approach to handling requests and responses, offering greater flexibility for various use cases beyond traditional web applications.

17,877

A Git-based CMS for Static Site Generators

Pros of Decap CMS

  • Focused on content management for static sites, providing a user-friendly interface for non-technical users
  • Supports a wide range of static site generators and frameworks
  • Offers a flexible and customizable content model

Cons of Decap CMS

  • Limited to content management, lacking the broader serverless capabilities of Workers SDK
  • May require additional setup and configuration for complex use cases
  • Less integrated with cloud infrastructure compared to Workers SDK

Code Comparison

Decap CMS configuration (YAML):

backend:
  name: git-gateway
  branch: main
collections:
  - name: "blog"
    label: "Blog"
    folder: "content/blog"
    create: true
    fields:
      - {label: "Title", name: "title", widget: "string"}
      - {label: "Body", name: "body", widget: "markdown"}

Workers SDK script (JavaScript):

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  return new Response('Hello worker!', {
    headers: { 'content-type': 'text/plain' },
  })
}

This comparison highlights the different focus areas of the two projects, with Decap CMS specializing in content management for static sites and Workers SDK providing a platform for serverless edge computing.

⚡ Serverless Framework – Effortlessly build apps that auto-scale, incur zero costs when idle, and require minimal maintenance using AWS Lambda and other managed cloud services.

Pros of Serverless

  • Supports multiple cloud providers (AWS, Azure, GCP, etc.)
  • Extensive ecosystem with plugins and integrations
  • Mature project with a large community and extensive documentation

Cons of Serverless

  • Steeper learning curve due to broader scope
  • Can be more complex to set up and configure
  • May have higher latency for certain use cases

Code Comparison

Serverless (serverless.yml):

service: my-service
provider:
  name: aws
  runtime: nodejs14.x
functions:
  hello:
    handler: handler.hello

Workers SDK (wrangler.toml):

name = "my-worker"
type = "javascript"
account_id = "your-account-id"
workers_dev = true
route = ""
zone_id = ""

Key Differences

  • Serverless focuses on multi-cloud deployments, while Workers SDK is specific to Cloudflare's edge network
  • Workers SDK offers simpler configuration for edge computing use cases
  • Serverless provides more flexibility for complex architectures across various cloud providers
  • Workers SDK enables faster cold starts and lower latency for certain scenarios
  • Serverless has a larger ecosystem of plugins and integrations

Both projects aim to simplify serverless deployments, but they cater to different use cases and deployment targets. Choose based on your specific requirements and preferred cloud infrastructure.

31,844

Fast and low overhead web framework, for Node.js

Pros of Fastify

  • High performance: Fastify is known for its speed and low overhead
  • Extensive plugin ecosystem: Offers a wide range of plugins for various functionalities
  • Highly extensible: Easy to add custom plugins and extend core functionality

Cons of Fastify

  • Steeper learning curve: More complex API compared to simpler frameworks
  • Less suitable for serverless: Not optimized for serverless environments like Cloudflare Workers

Code Comparison

Fastify:

const fastify = require('fastify')()

fastify.get('/', async (request, reply) => {
  return { hello: 'world' }
})

fastify.listen(3000)

Workers SDK:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  return new Response('Hello World!', { status: 200 })
}

Key Differences

  • Fastify is a traditional Node.js web framework, while Workers SDK is designed for Cloudflare's serverless platform
  • Fastify offers more built-in features and extensibility, whereas Workers SDK provides a simpler API tailored for edge computing
  • Fastify requires a server to run, while Workers SDK code runs directly on Cloudflare's edge network

Use Cases

  • Choose Fastify for traditional web applications with complex routing and middleware requirements
  • Opt for Workers SDK when building serverless applications designed to run on Cloudflare's global network
66,731

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀

Pros of Nest

  • More comprehensive framework with built-in support for various architectural patterns (MVC, CQRS, etc.)
  • Extensive ecosystem with many official and community-created modules
  • Strong TypeScript support and decorators for enhanced developer experience

Cons of Nest

  • Steeper learning curve due to its extensive feature set
  • Potentially heavier and slower for simple applications
  • Not specifically designed for serverless or edge computing environments

Code Comparison

Nest:

@Controller('cats')
export class CatsController {
  @Get()
  findAll(): string {
    return 'This action returns all cats';
  }
}

Workers SDK:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  return new Response('Hello worker!', { status: 200 })
}

The Nest example shows a typical controller setup with decorators, while the Workers SDK example demonstrates a basic request handler for a Cloudflare Worker. Nest provides a more structured approach with built-in routing, while Workers SDK offers a simpler, function-based approach tailored for edge computing.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Cloudflare Workers SDK

workers-logo
Cloudflare Workers let you deploy serverless code instantly across the globe for exceptional performance, reliability, and scale.

Contribute · Submit an Issue · Join Discord

Wrangler on npm   Discord conversation   X conversation


Quick Start

To get started quickly with a new project, run the command below:

npm create cloudflare@latest
# or
pnpm create cloudflare@latest
# or
yarn create cloudflare@latest

For more info, visit our Getting Started guide.

Documentation

Visit the official Workers documentation here.

Directory

PackageDescriptionLinks
wranglerA command line tool for building Cloudflare Workers.Docs
create-cloudflare (C3)A CLI for creating and deploying new applications to Cloudflare.Docs
miniflareA simulator for developing and testing Cloudflare Workers, powered by workerdDocs
wrangler-devtoolsCloudflare's fork of Chrome DevTools for inspecting your local or remote Workers
pages-sharedUsed internally to power Wrangler and Cloudflare Pages. It contains all the code that is shared between these clients.

Contributing

We welcome new contributors! Refer to the CONTRIBUTING.md guide for details.

Community

Join us in the official Cloudflare Discord to meet other developers, ask questions, or learn more in general.

Links

NPM DownloadsLast 30 Days