rp1 🚀

Blazingly fast and simple web framework for Deno, suitably named after the rocket fuel.

import Router from "https://deno.land/x/rp1/mod.ts";

const router = new Router();

router.get("/", () => "Hello World!");

Deno.serve(router.handle);

Features

Generates JSON for serializable values.

// '{ "data": [1, 2, 3] }' with a 200 status code
router.get("/json", () => { data: [1, 2, 3] });

Infers parameters from the path.

// 'params' is of type { id: string }
router.get("/users/:id", ({ params }) => {
    const id = params.id;
    // ...
});

Sane error handling.

import Router, { ServerError } from "https://deno.land/x/rp1/mod.ts";

// '{ "error": { "status": 418, "message": "I'm a teapot" } }'
router.get("/coffee", () => {
    throw new ServerError({
        status: 418,
        message: "I'm a teapot"
    });
});

You can return Response objects.

// 'Hello World!'
router.get("/hello", () => new Response("Hello World!"));

Easy to use middleware.

// Simple request logger
router.use(async ({ request }, next) => {
    const method = request.method;
    const url = new URL(request.url);
    const path = url.pathname;

    console.log(`${method} ${path}`);

    await next();
});

CORs middleware included.

import cors from "https://deno.land/x/rp1/middleware/cors.ts";

router.use(cors());

...which is easily configurable.

import cors, { echo } from "https://deno.land/x/rp1/middleware/cors.ts";
import Skip from "https://deno.land/x/rp1/middleware/skip.ts";

// Skip CORS for public routes
const isPublic: Skip = ({ request }) => {
    const url = new URL(request.url);
    return url.pathname.startsWith("/public");
};

router.use(cors({
    skip: isPublic, 
    origin: echo,
    methods: ["GET", "POST"],
    headers: "Content-Type",
    // ...
}));

Uses native Request object, so WebSockets are supported out of the box.

router.get("/ws", ({ request }) => {
    const { socket, response } = Deno.upgradeWebSocket(request);
    
    socket.onopen = () => {
        console.log("Socket opened!");
    };

    // ...

    return response;
});