abdullaharik / sunder

A minimal framework for Service Workers (such as Cloudflare Workers).

Home Page:https://sunderjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CI NPM badge Documentation

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.

Documentation

Read the documentation here, or check out the introductory blog post here.

Installation

npm i --save sunder
# or
yarn add sunder

Example

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));
});

Highlight feature: strict route parameters

Strict routes gif

Inspiration

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.

License

MIT

About

A minimal framework for Service Workers (such as Cloudflare Workers).

https://sunderjs.com

License:MIT License


Languages

Language:TypeScript 85.5%Language:JavaScript 11.8%Language:CSS 2.7%