Cloudflare
The Cloudflare Developer Platform is a suite of tools and services designed to help developers build and manage modern web applications. It offers various features to enhance performance, security, and reliability.
Key Features of Cloudflare Developer Platform
Workers
Cloudflare Workers is a serverless platform that executes JavaScript code at the network edge, resulting in faster performance and reduced latency by running closer to users. It uses V8 isolates, making it extremely lightweight and efficient.
To create a new worker project, run:
npm create cloudflare@latest -- worker0
cd worker0
This command generates the following file structure:
├── package.json
├── src
│ └── index.ts
├── test
│ ├── index.spec.ts
│ └── tsconfig.json
├── tsconfig.json
├── vitest.config.mts
├── worker-configuration.d.ts
└── wrangler.toml
In index.ts, the content is as follows:
export default {
async fetch(request, env, ctx): Promise<Response> {
return new Response('Hello World!');
},
} satisfies ExportedHandler<Env>;
Cloudflare’s API adheres to web standards, such as using the Response.
To start the server, run:
npx wrangler dev
Wrangler is the command-line interface for the Cloudflare Developer Platform.
To deploy, execute:
npx wrangler deploy
R2
R2 is a globally distributed object storage service that is highly scalable, durable, and cost-effective.
You can create a bucket using Wrangler:
npx wrangler r2 bucket create bucket0
To use the bucket, add a binding in wrangler.toml:
[[r2_buckets]]
binding = 'bucket'
bucket_name = 'bucket0'
You can access the bucket in your worker like this:
await env.bucket.put(key, req.body, {
httpMetadata: {
contentType: req.headers.get('content-type')
}
});
await env.bucket.get(key);
D1
D1 is Cloudflare’s native serverless database built on SQLite.
Create a D1 database using Wrangler:
npx wrangler d1 create db0
Add a binding in wrangler.toml:
[[d1_databases]]
binding = "db"
database_name = "db0"
database_id = "17b5fd39-701a-4b67-2103-3ea11d62be69"
To create a table, write the SQL commands in a file and execute it with Wrangler:
npx wrangler d1 execute moldable --local --file=./sql/files.sql
Use the --local flag to apply changes to the local database. When the schema is finalized, run the command again with the --remote flag.
Ad-hoc queries are supported with the --command option:
npx wrangler d1 execute moldable --local --command 'SELECT * FROM users;'
KV
KV is a distributed Key-Value Store that provides fast, reliable, and scalable storage for applications. It takes advantage of Cloudflare's global network, ensuring data is stored and distributed across multiple data centers.
To create a new namespace, run:
npx wrangler kv:namespace create kv0
Add a binding in wrangler.toml:
[[kv_namespaces]]
binding = "kv"
id = "0ae25e3203a0465de3c1a935e73fb92c"
Here is how to use it in your code:
await env.kv.put(key, JOSN.stringify(data))
await env.kv.get(key, {type: 'json'})
Pages
Cloudflare Pages is similar to Workers but is designed for dynamic front-end applications.