How does Express know which Router path to use when multiple paths match?

NathanSuzuki

Say there are 2 router.route(), for example:

router.route('/app/:id').get(funtion(req, res, next){
    console.log("id route")
});

and

router.route('/app/:username').get(funtion(req, res, next){
    console.log("user route")
});

When GET /app/nsuzuki is called, which router.route() does it use and why?

Chris Anderson-MSFT

To fully understand this, please read the documentation: http://expressjs.com/api.html#router.METHOD

How Express Routes Capture Paths

When you use a :param as a part of your path, it matches everything like /*, and the captured value is stored in req.params.param.

When you have more than one rule, the first one registered is the first one checked against. It checks each call against each rule until a match is found. If you call next() with no values passed to it, it will look for the next match (either in the same route, or continue on into the next middleware).

So these three rules will all be run

var handleRoute = function(req, res, next){
   console.log(req.path + ' ' + req.params.id + ' ' + req.params.user + ' ' + req.params[0]);
   next();
}

route.route('/user/:id').get(handleRoute);
route.route('/user/:user').get(handleRoute);
route.route('/user/*').get(handleRoute);

When I request /user/foobar, I'll see the following output (and probably an error as a client because I never responded :P)

/user/foobar foobar undefined undefined
/user/foobar undefined foobar undefined
/user/foobar undefined undefined foobar

It will hit all three, but the context is different for each.

How to Capture Path Patterns with Regular Expressions

If you want to capture separate routes for id (let's say all numbers) and user name (all letters), you can do this:

var handleRoute = function(tag) {
  return function(req, res, next) {
    console.log(tag + ' ' + req.path + ' -> ' + req.params[0]);
    res.status(200)
      .json({
        success: 'yay!'
      });
  };
};

route.route(/^\/user\/([0-9]+)$/i)
  .get(handleRoute('id'));
route.route(/^\user\/([A-Za-z]+)$/i)
  .get(handleRoute('user'));
route.route('/user/*')
  .get(handleRoute('catch all'));

Note the parathesis around my capture group. Without this, params is empty. It auto captures with just * in the string because they are nice folks. I'll get the following for output when I run against the three different types.

id /user/123 -> 123
user /user/user -> user
catch all /user/user.1 -> user.1

All that explained, you're opening yourself up to some vectors for bugs to infest your system. Might want to think about your URL pattern.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How does the system know which paths to search for executables?

From Dev

How does SSH know which key to use?

From Dev

Which git version does 'go get' use when multiple git are available in $PATH?

From Dev

How does Java 8 know which String::compareTo method reference to use when sorting?

From Dev

How to use unison's path option with multiple paths?

From Dev

How to use a middleware router for a sub path in express nodejs

From Dev

How does spring know which view resolver to use?

From Dev

At runtime, how does Swift know which implementation to use?

From Java

How does shared_ptr<void> know which destructor to use?

From Dev

How does git know which ssh key to use for its operations?

From Dev

How does this model factory know which method to use?

From Dev

How does a transparent SOCKS proxy know which destination IP to use?

From Dev

How does maven know which repo to use for a dependency?

From Dev

How does a transparent SOCKS proxy know which destination IP to use?

From Dev

How does spring know which view resolver to use?

From Dev

How does Rails 4 know which format to use for views?

From Dev

How does this model factory know which method to use?

From Dev

How does /usr/bin/env know which program to use?

From Dev

At runtime, how does Swift know which implementation to use?

From Dev

How does git know which ssh key to use for its operations?

From Dev

How does BIOS bootloader know which disk to use?

From Dev

How does Django know which model manager to use?

From Dev

How to know for which alias name, the module is loaded, when there are multiple aliases?

From Dev

How does Xcode know which app to replace when updating

From Dev

How does google know which onsubmit trigger to execute when a form is submitted if you have multiple forms sending responses to a single spreadsheet

From Dev

How does google know which onsubmit trigger to execute when a form is submitted if you have multiple forms sending responses to a single spreadsheet

From Dev

How to know which LayoutParams class to use when creating a View programmatically?

From Dev

How to know which 802.11 standard is used by a router

From Dev

How does the compiler determine which provider to use when using multiple Linq2 .... contexts?

Related Related

  1. 1

    How does the system know which paths to search for executables?

  2. 2

    How does SSH know which key to use?

  3. 3

    Which git version does 'go get' use when multiple git are available in $PATH?

  4. 4

    How does Java 8 know which String::compareTo method reference to use when sorting?

  5. 5

    How to use unison's path option with multiple paths?

  6. 6

    How to use a middleware router for a sub path in express nodejs

  7. 7

    How does spring know which view resolver to use?

  8. 8

    At runtime, how does Swift know which implementation to use?

  9. 9

    How does shared_ptr<void> know which destructor to use?

  10. 10

    How does git know which ssh key to use for its operations?

  11. 11

    How does this model factory know which method to use?

  12. 12

    How does a transparent SOCKS proxy know which destination IP to use?

  13. 13

    How does maven know which repo to use for a dependency?

  14. 14

    How does a transparent SOCKS proxy know which destination IP to use?

  15. 15

    How does spring know which view resolver to use?

  16. 16

    How does Rails 4 know which format to use for views?

  17. 17

    How does this model factory know which method to use?

  18. 18

    How does /usr/bin/env know which program to use?

  19. 19

    At runtime, how does Swift know which implementation to use?

  20. 20

    How does git know which ssh key to use for its operations?

  21. 21

    How does BIOS bootloader know which disk to use?

  22. 22

    How does Django know which model manager to use?

  23. 23

    How to know for which alias name, the module is loaded, when there are multiple aliases?

  24. 24

    How does Xcode know which app to replace when updating

  25. 25

    How does google know which onsubmit trigger to execute when a form is submitted if you have multiple forms sending responses to a single spreadsheet

  26. 26

    How does google know which onsubmit trigger to execute when a form is submitted if you have multiple forms sending responses to a single spreadsheet

  27. 27

    How to know which LayoutParams class to use when creating a View programmatically?

  28. 28

    How to know which 802.11 standard is used by a router

  29. 29

    How does the compiler determine which provider to use when using multiple Linq2 .... contexts?

HotTag

Archive