Giter Site home page Giter Site logo

Comments (2)

jonchurch avatar jonchurch commented on April 27, 2024 1

im closing this as it's not something I expect us to seriously prioritize

from discussions.

jonchurch avatar jonchurch commented on April 27, 2024

for reference it was this issue, and the dev had a good outcome from my human written but LLM assisted debugging: pillarjs/routington#29

the output the LLM gave me with just their issue (which was a pretty dang good issue, it included enough code to know exactly what they were doing):

The issue you're encountering with matching a query string (/name?age=50) using routington is because routington is designed to match URL paths, not query strings. The query string is not considered part of the URL path; it's a separate component used to pass additional data to the server. Therefore, when you try to match /name?age=50, it won't work as expected because routington is looking for a path that matches exactly, without considering the query string.

To fix this issue, you need to separate the path from the query string before matching. You can use the URL or another method to parse the incoming request URL, extract the path without the query string, and then pass it to your match function. Here's how you could adjust the dispatcher function to handle this:

 function dispatcher(request: { path: string; method: string }): { param: string; handler: Function } | string | never {
  // Extract path without query string
  const url = new URL(request.path, `http://${request.headers?.host || 'localhost'}`);
  const pathWithoutQueryString = url.pathname;

  const match = router.match(pathWithoutQueryString);
  if (!match) { console.log('404 - Not Found'); return "" };

  const node = match.node;
  const cb = node[request.method];

  if (!cb) { console.log('405 - Method Not Allowed'); return ""; }

  return {
    param: match.param,
    handler: cb // You might need to check if cb is not undefined before calling it
  }
}

This adjustment uses the URL object to parse the incoming request.path, extracting the pathname (which excludes the query string) before it's passed to router.match. Note that this example assumes request.path includes the full URL; you might need to adjust this based on how your actual request object is structured. If request.path already contains only the path, you won't need to prepend a base URL.

Also, ensure you handle cases where the method does not exist on the matched route to avoid runtime errors.

from discussions.

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.