Going originless with Cloudflare Workers – Building a Todo app – Part 1: The API

Kabir Sikand
8 min readbeginner
--
View Original

Overview

This article introduces the process of building a serverless Todo application using Cloudflare Workers and Custom Domains. It details the architecture, including the use of a D1 database and API Gateway to manage requests, emphasizing the benefits of a globally distributed application without the need for an origin server.

What You'll Learn

1

How to create a serverless API using Cloudflare Workers

2

Why using Custom Domains simplifies application deployment

3

How to set up a D1 database for a Todo application

4

When to utilize an API Gateway for microservices

Prerequisites & Requirements

  • Basic understanding of serverless architecture and APIs
  • Familiarity with Cloudflare Workers and D1 database(optional)

Key Questions Answered

What is the purpose of Custom Domains in Cloudflare Workers?
Custom Domains allow developers to connect their Workers to the Internet without managing DNS records or certificates. This feature enables a seamless deployment experience and facilitates building globally distributed applications that leverage Cloudflare's network.
How do you bootstrap a D1 database for a Todo application?
To bootstrap a D1 database, use the command 'npx wrangler d1 create <database-name>' to create a new database. Then, define the schema in a SQL file and execute it with 'npx wrangler d1 execute <database-name> --file=./schema.sql' to set up the initial data.
How can you build an API Gateway with Cloudflare Workers?
An API Gateway can be built by creating a Worker that routes incoming requests to the appropriate microservice based on the URL pathname and HTTP method. This allows for a centralized entry point for managing multiple microservices effectively.
What are the benefits of using Cloudflare's global network for applications?
Using Cloudflare's global network allows applications to be serverless and distributed, eliminating the need for an origin server. This architecture enhances scalability, reduces latency, and simplifies deployment and maintenance for developers.

Technologies & Tools

Backend
Cloudflare Workers
Used to create serverless functions that handle API requests.
Database
D1
Cloudflare's serverless database used to store and manage Todo application data.

Key Actionable Insights

1
Utilize Custom Domains to streamline your deployment process.
By using Custom Domains, developers can avoid the complexities of DNS management, allowing them to focus on building features rather than infrastructure.
2
Implement an API Gateway to manage microservices efficiently.
An API Gateway centralizes request handling, making it easier to manage and scale microservices independently, which is crucial for maintaining a clean architecture.
3
Leverage the D1 database for serverless applications.
D1 provides a simple way to manage data without the overhead of traditional database management, making it ideal for serverless applications like the Todo app.

Common Pitfalls

1
Neglecting to define proper routing in the API Gateway can lead to unsupported method errors.
Ensure that all expected HTTP methods are handled correctly in the routing logic to avoid 405 errors when clients interact with the API.

Related Concepts

Serverless Architecture
Microservices
API Design
Cloudflare Services