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.
- 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.
bun i github:NobleMajo/bunrouter
import { Router } from "bunrouter/src/index";
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))
<<<<<<< Updated upstream Checkout the bunrouter examples:
- simple example
- static-serve example
- websocket example
- redirect example
- cookies example ======= Checkout the bun-router examples:
- simple example
- static-serve example
- websocket example
- redirect example
- cookies example
- basic auth example
Stashed changes
Run a example:
git clone https://github.com/NobleMajo/bunrouter.git
bun run examples/simple.ts
The router has build-in tests.
bun test
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.
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.
This project is licensed under the MIT license, providing users with flexibility and freedom to use and modify the software according to their needs.
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.