bunway
Version:
Express-style routing toolkit built natively for Bun.
46 lines • 1.3 kB
JavaScript
import { Router } from "./core/router";
/**
* Default port for bunWay apps. Easy to remember, low collision.
*/
export const BUNWAY_DEFAULT_PORT = 7070;
/**
* BunWayApp extends the Router with an Express-style .listen().
* Usage:
* const app = bunway();
* app.get("/", (ctx) => ctx.res.text("OK"));
* app.listen(7070);
*/
export class BunWayApp extends Router {
constructor(options) {
super(options);
}
/**
* Start the HTTP server.
* - listen(port)
* - listen({ port, hostname })
* - listen(port, onListen)
* - listen(options, onListen)
*/
listen(portOrOptions, onListen) {
const port = typeof portOrOptions === "number"
? portOrOptions
: (portOrOptions?.port ?? BUNWAY_DEFAULT_PORT);
const hostname = typeof portOrOptions === "object" ? portOrOptions.hostname : undefined;
const server = Bun.serve({
port,
hostname,
fetch: (req) => this.handle(req),
});
// Optional callback (Express-style)
if (onListen)
onListen();
return server;
}
}
/**
* Factory to create a bunWay app (Express-style).
*/
export function bunway(options) {
return new BunWayApp(options);
}
//# sourceMappingURL=server.js.map