talentlessguy/tinyhttp

Some router paths don't work if router is mounted after defining routes #278

weedz posted onGitHub

Describe the bug

Can not find the correct route when using sub-apps.

To Reproduce

Steps to reproduce the behavior:

The following code does not work as intended:

import { App } from '@tinyhttp/app';

const app = new App()

const router = new App();
router.get("/list", (req,res) => {
    console.log("router/list");
    res.send("router/list");
});
router.get("/find", (req,res) => {
    console.log("router/find");
    res.send("router/find");
});
app.use("/router", router);

app.listen(3000);

I can't request /router/find but /router/list works.

If I instead "use" router before we define any routes it works:

import { App } from '@tinyhttp/app';

const app = new App()

const router = new App();
app.use("/router", router); // <-- HERE, we "use" router before we add any routes
router.get("/list", (req,res) => {
    console.log("router/list");
    res.send("router/list");
});
router.get("/find", (req,res) => {
    console.log("router/find");
    res.send("router/find");
});

app.listen(3000);

Expected behavior

Should not matter where the use is called? When requesting /router/find you should see router/find instead of Not Found.

Versions

  • node: 14.16.1
  • @tinyhttp/app: 1.3.11

Additional context

Add any other context about the problem here.


@talentlessguy has funded $5.00 to this issue.


posted by issuehunt-app[bot] almost 4 years ago

This might not be the correct way to solve this (and might possibly break other things?) but in the App.use function, if we map over all the middlewares instead of only the first one it seems to work.

pushMiddleware(this.middleware)({
  path: base as string,
  regex,
  type: 'mw',
  handler: mount(fns[0] as Handler),
  handlers: fns.slice(1).map(mount),
  fullPaths: fns
  .flat()
  .map((fn) =>
-    fn instanceof App && fn.middleware?.[0] ? lead(base as string) + lead(fn.middleware?.[0].path)  : ''
+    fn instanceof App && fn.middleware?.length ? fn.middleware.map(mv => lead(base as string) + lead(mv.path))  : ''
  )
})
posted by weedz almost 4 years ago

@weedz could you please fork it and check on some unit tests for router?

posted by talentlessguy almost 4 years ago

@weedz could you please fork it and check on some unit tests for router?

Sure

posted by weedz almost 4 years ago

Does not seem to work. Fails at

Testing App routing › should can set url prefix for the application

Returns "Not Found" for /abc/route2 and /abc/route3

posted by weedz almost 4 years ago

@talentlessguy has rewarded $4.50 to @sbencoding. See it on IssueHunt

  • :moneybag: Total deposit: $5.00
  • :tada: Repository reward(0%): $0.00
  • :wrench: Service fee(10%): $0.50
posted by issuehunt-app[bot] over 3 years ago

Fund this Issue

$5.00
Rewarded

Rewarded pull request

Recent activities

sbencoding was rewarded by talentlessguy for talentlessguy/tinyhttp# 278
over 3 years ago
sbencoding submitted an output to  talentlessguy/ tinyhttp# 278
over 3 years ago