Sunder is a minimal server-side framework for ServiceWorker environments. It is intended for websites and APIs built on Cloudflare Workers.
Think of it as Express or Koa for serverless which allows you to quickly build websites and APIs in a modern async structure. Also important: it is easy to write tests for.
The easiest way to get started with Sunder on Cloudflare Workers is to use the template project.
Read the documentation here, or check out the introductory blog post here.
npm i --save sunder
# or
yarn add sunder
import {Sunder, Router, Context} from "sunder";
const app = new Sunder();
const router = new Router();
// Example route with a named parameter
router.get("/hello/:username", ({response, params}) => {
response.body = `Hello ${params.username}`;
});
// Example middleware
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.response.set('X-Response-Time', `${ms}ms`);
});
app.use(router.middleware);
addEventListener('fetch', (event) => {
const fe = event as FetchEvent; // Only required in Typescript
fe.respondWith(app.handle(fe));
});
The Sunder framework was inspired by Elixir's plug, Node's Koa framework, tiny-request-router and cfworker's web package.
Sunder is only a few hundred lines of code. It has little magic and doesn't impose too much structure.
MIT