work with routes() helper

This commit is contained in:
Chris Wanstrath 2025-10-22 14:54:17 -07:00
parent 8268fd13ae
commit 84b001e651
2 changed files with 10 additions and 5 deletions

View File

@ -48,8 +48,8 @@ export function routes(def: Record<string, Handler>): Hono {
for (const key in def) {
const parts = key.split(" ") // GET /path
const method = parts[0] || "GET"
const path = parts[1] || "/"
const method = (!parts[0] || parts[0].startsWith("/")) ? "GET" : parts[0]
const path = (parts[0]?.startsWith("/") ? parts[0] : parts[1]) || "/"
//@ts-ignore
app.on(method, path, async c => toResponse(await def[key](c)))

View File

@ -22,13 +22,18 @@ try {
}
const handler = mod.default
if (typeof handler !== "function") {
let app: Hono
if (typeof handler === "function") {
app = new Hono
app.all("*", async c => toResponse(await handler(c)))
} else if (handler && typeof handler === "object" && handler.routes && handler.router) {
app = handler
} else {
webappLog(appName, `no default export`)
process.exit(1)
}
const app = new Hono()
app.all("*", async c => toResponse(await handler(c)))
const port = Number(process.env.PORT || 4000)
Bun.serve({ port, fetch: app.fetch })