Skip to content

A bun serve router, that looks a bit like express.

License

Notifications You must be signed in to change notification settings

NobleMajo/bunrouter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MIT typescript

bunrouter

A fast express-like router written for the bun.serve http server. Uses the bun.js high-performance bun.serve() http server for a fast, familiar and solid routing experience.

Bun does not have a built-in router, so I created this one without any runtime dependencie.

features

  • non-async-first: Tries to resolve a request in a non-async way until an async handler is hit.
  • wildcards: Can handle double wildcards (**) as any recursive path and single wildcards (*) as any path part. It also provides a path params string array.
  • cookie-handling: Cookie parsing can be enabled via a middleware (example).
    If enabled cookies can also automatically be set/unset to the response headers.
  • static-serve: Serves static files via a middleware (example).
  • redirect-handler: You can redirect via the ResponseBuilder or <<<<<<< Updated upstream via a redirect middleware (example).
  • websocket-support: Can handle websocket request via a middleware (example). ======= via a redirect middleware (example).
  • websocket-support: Can handle websocket request via a middleware (example).
  • basic-auth: Protects the following via http basic auth (example).

Stashed changes

  • merged-routes: If 2 or more routes are defined one after the other with the same method and path, they will be merged into a single handler to avoid re-checking.
  • method-enum: You just write GET, but in the background it is converted to an enum.
    This is for faster method comparison.
  • dump-router: You can create a string router dump that lists the defined routes. If you provide a bun server, it also adds a server-is-running-on message.

how it works

usage

install

bun i github:NobleMajo/bunrouter

import

import { Router } from "bunrouter/src/index";

example

import { Router } from "bunrouter/src/index";

const router = new Router()

// handles GET requests to /
router.get("/", (req, res) => {
    res.send("Root request")
})

export const server = Bun.serve({
    fetch: router.handle,
})

// dumps router routes and server-is-running-on message
console.info(router.dump(server))

examples

<<<<<<< Updated upstream Checkout the bunrouter examples:

Stashed changes

Run a example:

git clone https://github.com/NobleMajo/bunrouter.git

bun run examples/simple.ts

tests

The router has build-in tests.

bun test

future features

Here are some feature ideas for future development:

  • CORS-support: Configure CORS headers via a buildin middleware.
  • router.serve: Serve/listen function to start the server via the router (first parameter should be the bun.serve options without fetch).
  • websocket: Better websocket support with more origin request infos.

Contributing

Contributions to this project are welcome!
Interested users can refer to the guidelines provided in the CONTRIBUTING.md file to contribute to the project and help improve its functionality and features.

License

This project is licensed under the MIT license, providing users with flexibility and freedom to use and modify the software according to their needs.

Disclaimer

This project is provided without warranties.
Users are advised to review the accompanying license for more information on the terms of use and limitations of liability.

About

A bun serve router, that looks a bit like express.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published