
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
bun-serve-router
Advanced tools
A very simple router implementation for `bun.serve()` using [URL Pattern API](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern).
A very simple router implementation for bun.serve()
using URL Pattern API.
No fancy, just works.
/user/:id
Request
and Response
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;
}
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 });
},
});
router.add("GET", "/hello/:myName", (request, params) => {
return new Response("Hello " + params.myName);
})
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);
})
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.
FAQs
A very simple router implementation for `bun.serve()` using [URL Pattern API](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern).
The npm package bun-serve-router receives a total of 8 weekly downloads. As such, bun-serve-router popularity was classified as not popular.
We found that bun-serve-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.