diff --git a/package.json b/package.json index 8ae7391..00d502c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bunrest", - "version": "1.3.4", + "version": "1.3.5", "main": "dist/index.js", "types": "dist/index.d.ts", "description": "An express-like API for bun http server", diff --git a/src/server/server.ts b/src/server/server.ts index 566537c..514d5a7 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -108,7 +108,7 @@ class BunServer implements RequestMethod { else { if (arg1.length === 3) { this.middlewares.push({ - path: "/", + path: "*", middlewareFunc: arg1 as Handler, }); } else if (arg1.length === 4) { @@ -149,20 +149,10 @@ class BunServer implements RequestMethod { async fetch(req1: Request) { const req: BunRequest = await that.bunRequest(req1); const res = that.responseProxy(); - const tree: TrieTree = - that.requestMap[req.method.toLowerCase()]; - - if (!tree) { - throw new Error(`There is no path matches ${req.method}`); - } - - const leaf = tree.get(req.path); - const handlers: Handler[] = leaf.node?.getHandlers(); - // append req route params - req.params = leaf.routeParams; + // middlewares handler if (that.middlewares.length !== 0) { - const plainMid = that.middlewares.filter((mid) => mid.path === "/"); + const plainMid = that.middlewares.filter((mid) => mid.path === "*"); const chain = new Chain(req, res, plainMid); chain.next(); @@ -178,7 +168,7 @@ class BunServer implements RequestMethod { const middlewares = []; for (let i = that.middlewares.length - 1; i >= 0; --i) { const target = that.middlewares[i]; - if (target.path === "/") { + if (target.path === "*") { continue; } @@ -201,6 +191,19 @@ class BunServer implements RequestMethod { } } + // request handler + const tree: TrieTree = + that.requestMap[req.method.toLowerCase()]; + + if (!tree) { + throw new Error(`There is no path matches ${req.method}`); + } + + const leaf = tree.get(req.path); + const handlers: Handler[] = leaf.node?.getHandlers(); + // append req route params + req.params = leaf.routeParams; + // fix (issue 4: unhandle route did not throw an error) if (!handlers || handlers.length === 0) { throw new Error(`Cannot ${req.method} ${req.path}`);