Code Monkey home page Code Monkey logo

Comments (3)

jlalmes avatar jlalmes commented on September 28, 2024

I have a feeling there will not be a clean solution to this.

Option 1: Try to resolve out all the paths inside the express.Routers from the req.app._router.stack array. Have looked into this a little - seems to get messy, fast!

Below is just pseudo-code for what that might look like:

const getRoutesOfLayer = (layer, path = '') => {
  if (layer && layer.route && layer.route.path && layer.regexp) {
    layer.route.path = path + layer.route.path;
    return [layer];
  }
  if (layer && layer.name === 'router' && layer.handle && layer.handle.stack && layer.path) {
    return layer.handle.stack.reduce((acc, routerLayer) => {
      return [
        ...acc,
        ...getRoutesOfLayer(routerLayer, path + layer.path),
      ];
    }, []);
  }
  return [];
};

matchedRouteMiddleware = req.app._router.stack
  .reduce((acc, layer) => {
    return [
      ...acc,
      ...getRoutesOfLayer(layer),
    ];
  }, [])
  .filter((middleware) => {
    // Check if the URL matches the route
    const isMatch = middleware.regexp.test(preQueryUrl);

    // Add matches in the hope that common routes will be faster than searching everything
    if (isMatch) { commonRouteMiddlewares.push(middleware); }

    return isMatch;
  })[0];

Option 2: Ditch the whole idea of express middleware and instead extend the express.Route.prototype.dispatch prototype.

Again, the below is just pseudo-code for what that might look like:

const defaultImplementation = express.Route.prototype.dispatch;
express.Route.prototype.dispatch = function handle(req, res, next) {  
  // req.route.path is now available
  handler(req, res);
  defaultImplementation.call(this, req, res, next);
};

^ To note - although the handler now has access to req.route.path, it does not include the path of the express.Router that it might sit under.

Not imagining this is going to be a fast fix - but keen to be kept in the loop!

from scout_apm_node.

 avatar commented on September 28, 2024

Hi @provinceinnovation thanks for reporting this -- the path will be more likely the second option, with some tracking if necessary to get the routes' path if needed.

from scout_apm_node.

 avatar commented on September 28, 2024

Hey @provinceinnovation so I got a chance to look at this and realized that what's happening is that the request is getting captured (I see requests being sent to core-agent) but they're not marked up with the path and url tags that they should be, so the request is actually being ignored there (we ignore any path that we can't determine a url/path for).

As you surmised (I think) the problem is that the URL can't be determined from the router, but the approach I'm taking is shimming the router itself (much like your second option) -- I'll be either Router#handle or Router#[HTTP verb] if I can't get everything I'd need from handle.

from scout_apm_node.

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.