Giter VIP home page Giter VIP logo

Comments (19)

aarontravass avatar aarontravass commented on June 2, 2024 1

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)

from tinyhttp.

aarontravass avatar aarontravass commented on June 2, 2024 1

Thanks for raising this issue.

from tinyhttp.

aarontravass avatar aarontravass commented on June 2, 2024 1

@atif-saddique-deel I pushed a fix to https://github.com/aarontravass/tinyhttp/tree/aaron/route

Can you try it out once?

from tinyhttp.

talentlessguy avatar talentlessguy commented on June 2, 2024 1

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.

from tinyhttp.

aarontravass avatar aarontravass commented on June 2, 2024 1

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

from tinyhttp.

aarontravass avatar aarontravass commented on June 2, 2024 1

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

if (typeof base === 'function' || base instanceof App) {
fns.unshift(base)
} else {
// if base is not an array of paths, then convert it to an array.
let basePaths = []
if (Array.isArray(base)) basePaths = [...base]
else if (typeof base === 'string') basePaths = [base]
basePaths = basePaths.filter((element) => {
if (typeof element === 'string') {
pathArray.push(element)
return false
}
return true
})
fns.unshift(...basePaths)
}

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

from tinyhttp.

aarontravass avatar aarontravass commented on June 2, 2024

image

from tinyhttp.

atif-saddique-deel avatar atif-saddique-deel commented on June 2, 2024

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

from tinyhttp.

aarontravass avatar aarontravass commented on June 2, 2024

Yes, I'll raise a PR and get it updated.

from tinyhttp.

atif-saddique-deel avatar atif-saddique-deel commented on June 2, 2024

I have tested and it doesn't seem to work as expected, it always return the first route, not the current route.
I am going to create a codesandbox demo example and share it in a while here.

from tinyhttp.

atif-saddique-deel avatar atif-saddique-deel commented on June 2, 2024

Here is the example to reproduce issue : https://codesandbox.io/p/sandbox/flamboyant-mirzakhani-cvndqy
Steps:

  1. Open this codesandbox link
  2. Hit this url in internal browser on right side in codesandbox: https://cvndqy-5500.csb.app/user/23/api
  3. You will see that it returns route path of first initialised route only.
  4. In order to confirm this, you can change the order of routes and put the ping route after user/:id/api
Screenshot 2023-09-26 at 7 53 30 PM

from tinyhttp.

aarontravass avatar aarontravass commented on June 2, 2024

Ok so the route matching technique is kinda faulty. It needs to check by the fullPath rather than handler name

export const getRouteFromApp = ({ middleware }: App, h: Handler<Request, Response>): Middleware<Request, Response> =>
middleware.find(({ handler }) => typeof handler === 'function' && handler.name === h.name)

from tinyhttp.

talentlessguy avatar talentlessguy commented on June 2, 2024

@aarontravass you can already submit a PR and I'll check out locally and run tests

if CI passes, we're good

we can also add additional tests

from tinyhttp.

atif-saddique-deel avatar atif-saddique-deel commented on June 2, 2024

@aarontravass is there a new minor version or something which I can use to test or I have to run locally and build it in order to test?

from tinyhttp.

atif-saddique-deel avatar atif-saddique-deel commented on June 2, 2024

by the way, setup says to use node 16 but its soon going to meet its end of life, so maybe it should be 18 atleast.
https://nodejs.org/en/blog/announcements/nodejs16-eol

from tinyhttp.

atif-saddique-deel avatar atif-saddique-deel commented on June 2, 2024

Its working as expected.
Thanks.

http://localhost:5400/hello/2/api/45

app.get('/hello/:id/api/:user', (req, res) => {
  res.json({
    params: req.params,
    route: req.route,
    path: req.path,
    url: req.url
  })
})
Screenshot 2023-09-28 at 12 16 54 PM

from tinyhttp.

atif-saddique-deel avatar atif-saddique-deel commented on June 2, 2024

@talentlessguy @aarontravass this issue is automatically closed but there is no new version available on npm with fix.
Could you please push the newer version to npm so that we can start using it?

from tinyhttp.

aarontravass avatar aarontravass commented on June 2, 2024

Hey @atif-saddique-deel, the new version has been released as 2.2.0. Please check it out
https://github.com/tinyhttp/tinyhttp/releases/tag/%40tinyhttp%2Fapp%402.2.0
https://www.npmjs.com/package/@tinyhttp/app

Apologies for the inconvenience.

from tinyhttp.

atif-saddique-deel avatar atif-saddique-deel commented on June 2, 2024

It solved the problem in main route but the issue in sub routes is still there.
Can we please reopen the issue?
I have updated the demo sandbox with sub route.
so If you hit this url: https://cvndqy-3000.csb.app/sub-route/user/23/api
you will see that the top level route is missing from route.path

Screenshot 2023-09-29 at 10 00 14 AM
import { App } from "@tinyhttp/app";

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.ur,
  });
});

app.get("/", (_, res) => {
  res.send("Its working");
});
// ------- sub route -------------
// sub route will probably be in different file, but I put here for demo

const subRoute = new App({ settings: { enableReqRoute: true } });
app.use("/sub-route", subRoute);

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

// -------end sub route ------------

const port = "3000";

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

cc @aarontravass @talentlessguy

from tinyhttp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.