Cloudflare Docs
Durable Objects
Visit Durable Objects on GitHub
Set theme to dark (⇧+D)

Durable Objects Rollup ES modules template

Create Durable Objects Rollup using ES modules template.

export class Counter {
constructor(state, env) {
this.state = state;
// Handle HTTP requests from clients.
async fetch(request) {
// Apply requested action.
let url = new URL(request.url);
// Durable Object storage is automatically cached in-memory.
// Reading the same key every request is fast.
// You could also store the value in a class member.
let value = await"value") || 0;
switch (url.pathname) {
case "/increment":
case "/decrement":
case "/":
// Serves the current value.
return new Response("Not found", {status: 404});
// You do not have to worry about a concurrent request having modified the value in storage.
// "input gates" will automatically protect against unwanted concurrency.
// Read-modify-write is safe.
await"value", value);
return new Response(value);

Find the full code for this example on GitHub.