After that run the server and open your browser http://localhost:3000
bun main.ts
Routing
Routes are added automatically when creating files inside your routesPath when exporting functions with the corresponding Method Names.
Given example above create index.ts inside routes/ and export a GET function calling ctx.sendResponse().
import {Context} from "@kapsonfire/bun-bakery"export async function GET(ctx: Context) { ctx.sendResponse(new Response('hello world!'));}
Parameters
Routes can have parameters inside dirname and/or filename. Just put the parameter name inside brackets and it will be added to ctx.params.
In example: given routes/user/[username].ts and open http://localhost:3000/user/kapsonfire
import {Context} from "@kapsonfire/bun-bakery"export async function GET(ctx: Context) { ctx.sendResponse(new Response('hello '+ ctx.params.username +'!'));}
will output hello kapsonfire!
Spread Paramaters
Routes can also have wildcard/spread paramaters.
In example: given routes/users/[...usernames].ts and open http://localhost:3000/users/kapsonfire/jarred/tricked
import {Context} from "@kapsonfire/bun-bakery"export async function GET(ctx: Context) { ctx.json(JSON.stringify(ctx.params));}
will output
{"usernames":["kapsonfire","jarred","tricked"]}
Websocket Server
Bun-Bakery supports websocket endpoint export for Bun.serve({...}).
Just export WEBSOCKET as Object registering the websocket hooks.
In example: given routes/websocket/user.ts