lau1944 / bunrest

An express-like API for bun server
MIT License
285 stars 31 forks source link

Trailslashes removing #32

Closed ammarhashad closed 11 months ago

ammarhashad commented 1 year ago

Hello, Great work with this package, really appreciate your effort

when i am trying

router.get("/", (req, res) => ...)

and import router to sever

app.use("/user" ,router)

you have to send your request to "user/" and only "user" will not work


router.ts

private delegate(localPath: string, method: string, handlers: Handler[]) { // Remove trailing slash if any localPath = localPath.endsWith("/") ? localPath.slice(0, -1) : localPath

for (let i = 0; i < handlers.length; ++i) {
  const handler = handlers[i]
  if (i == handlers.length - 1) {
    this.submitToMap(method.toLowerCase(), localPath, handler)
    break
  }

trie-tree.ts

private dig( node: Node<k, v>, paths: string[], params: { [key: string]: any } ): Node<k, v> | null { // remove trailing slashs paths = paths.filter((path, index) => path !== "" || index < 2)

if (paths.length === 0) {
  return node
}

and this will simulate the expressjs behavior

lewisjr commented 11 months ago

Noticed this as well, for now I just do this:

router.get("", async (req, res) => {
   // code and stuff
});

app.use("/path", router);

At least until this is patched.

lau1944 commented 11 months ago

Should work now