Giter VIP home page Giter VIP logo

bun-serve-router's Introduction

bun-serve-router

A very simple router implementation for bun.serve() using URL Pattern API.

No fancy, just works.

  • Supports URL patterns such as /user/:id
  • Using the standard API:

How to Use

Install the dependency:

bun add bun-serve-router

Import and add your routes:

import {Router} from "bun-serve-router";
const router = new Router();
router.add("GET", "/", (request, params) => {
    return new Response("Hello World!");
})

In the fetch handler of Bun.server(), you can match your routes like this:

const response = await router.match(request);

Since it is possible that no route matches, you should check if response is not undefined before returning it:

if (response) {
    return response;
}

Full Examples

import {Router} from "bun-serve-router";

const router = new Router();

// Add your routes
router.add("GET", "/", (request, params) => {
    return new Response("Hello World!");
})

Bun.serve({
    async fetch(request) {
        // Match here
        const response = await router.match(request);
        if (response) {
            return response;
        }

        // Return 404 if no route matches
        return new Response("404 Not Found", { status: 404 });
    },
});

Advanced Usage

Params

router.add("GET", "/hello/:myName", (request, params) => {
    return new Response("Hello " + params.myName);
})

URLPatternResult

The third parameter is the URLPatternResult object.

Check the URL Pattern API for more information.

router.add("GET", "/hello/:myName", (request, params, urlPatternResult) => {
    return new Response("Hello " + params.myName);
})

Others

It is actually a very standalone router. It is actually not limited to bun.serve(). As long as your application is using the Fetch API, it should be working too.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.