tinyhttp: req.route always returns undefined

Describe the bug

req.route always returns undefined https://tinyhttp.v1rtl.site/docs#reqroute

To Reproduce

Steps to reproduce the behavior:

  1. Just follow docs as explained here.
  2. Add the endpoint from docs and display req.route
  3. Its always undefined.

Expected behavior

The output should be similar to something written in docs.

{
  path: '/user/:id?',
  method: 'GET',
  handler: [Function: userIdHandler],
  type: 'route'
}

Versions

  • node: v16.20.0
  • @tinyhttp/app: 2.0.33

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 19 (11 by maintainers)

Most upvoted comments

Hey @atif-saddique-deel the implementation is correct and I’ll explain why but first, change the subRoute.get to the following

subRoute.get("/users/:id/api", (req, res) => {
  res.json({
    params: req.params,
    route: req.route,
    path: req.path,
    url: req.ur,
  });
});

image

Notice that the route.path is now users which means it is assigned correctly. Your code was still showing the correct route but 2 of the paths were identical.

Now coming to the why, when mounting an app, we don’t prepend each path with the mount point. Rather, we let the subapp handle the routing. Essentially, the path you see is the path in the subapp not the full path. The full path, with the subapp mount point prepended, is req.url which is essentially the URL itself. This can be seen in

https://github.com/tinyhttp/tinyhttp/blob/65cd2f12520ec62186d5bb4511af7b5daba3b0f2/packages/app/src/app.ts#L216-L232

hope this helps! let me know if you have any more questions.

A minor version will be released once the pr is merged but you can build my branch and create a new file at the root which references App from ./packages/app/src/app.ts. Like this

import { App } from "./packages/app/src/app.ts";

const app = new App({ settings: { enableReqRoute: true } });

app.get("/ping", (_, res) => {
  res.send("pong");
});

app.get("/user/:id/api", (req, res) => {
  res.json({
    params: req.params,
    route: req.route,
    path: req.path,
    url: req.url,
  });
});

app.get("/", (_, res) => {
  res.send("Its working");
});

const port = 5500;

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

To run this, run the following command

pnpx tsx watch server.ts

To build, here’s a guide https://github.com/aarontravass/tinyhttp/blob/aaron/route/CONTRIBUTING.md#local-setup

maybe that should be added into the docs then. Thanks for quickly replying, I am going to test it out.

true, there should be a notice that it can be enabled with enableReqRoute. Docs need a fix.

Thanks for raising this issue.

You need to enable req route by setting enableReqRoute to true in settings. See below

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

const app = new App({ settings:{ enableReqRoute: true }})

app.get('/user/:id?', function userIdHandler(req, res) {
  console.log(req.route)
  res.send('GET')
})

app.listen(3000)