Skip to content

Commit

Permalink
fix: no path found error
Browse files Browse the repository at this point in the history
  • Loading branch information
lau1944 committed Sep 17, 2023
1 parent d19419f commit 5b58a76
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
31 changes: 17 additions & 14 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<string, Handler> =
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();

Expand All @@ -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;
}

Expand All @@ -201,6 +191,19 @@ class BunServer implements RequestMethod {
}
}

// request handler
const tree: TrieTree<string, Handler> =
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}`);
Expand Down

0 comments on commit 5b58a76

Please sign in to comment.