0
router.post(/^\/(path_A|path_B)/, verify, async (req, res) => {
  ...
});

The goal is to match the path path_A and path_B in the same route. I don't think that the regex is written correctly. Can someone double check?

  • Did you try it? What did it match? Per the last example in [this article](https://www.kevinleary.net/regex-route-express/), yours looks like it would work. – jfriend00 Oct 30 '21 at 04:40
  • Is the `^` necessary, though? – Bear Bile Farming is Torture Oct 30 '21 at 04:46
  • See https://stackoverflow.com/questions/10858005/regex-for-route-matching-in-express for some examples. – jfriend00 Oct 30 '21 at 04:52
  • If you don't have `^` at the start, then the match won't be required to start at the beginning of the path. If you don't have `$` at the end, then the match won't be required to go all the way to the end of the path. For example, what you have would probably also match `/path_AAAAA`. – jfriend00 Oct 30 '21 at 05:04
  • See examples in the Express doc here: http://expressjs.com/en/guide/routing.html#route-paths. Note that if you use a string instead of a regex object, then Express adds things to the regex (like match from the beginning) and your regex options are more limited. – jfriend00 Oct 30 '21 at 05:22

1 Answers1

1

Your code:

router.post(/^\/(path_A|path_B)/, verify, async (req, res) => {
  ...
});

Will work just fine to match either /path_A or /path_B. I've verified it by running it locally. Some notes on this:

  1. The ^ is required if you want to ensure your regex starts at the beginning of the path and cannot match only something in the middle of the path. For example, without the ^, it would match /X/path_A or /www/path_B.

  2. Your existing regex will also match /path_AAAA and /path_Bxxx because it does not specify anything after the match. If you only wanted to match just /path_A or /path_B with nothing afterwards, then you could put a $ at the end of your regex. These are just regular regexes so they will just match what any old regex would match. All the normal regex rules apply.

  3. If you specify a string instead of a regex, then your matching options are more limited (to a subset of regex stuff that the path to regex library supports and Express will require more complete matches by testing for additional conditions. But, if you use a regex object, then all of that is up to you.


FYI, I tried going without the native regex object like this and letting the built-in path-to-regexp handle the details:

router.post("/(path_A|path_B)", verify, async (req, res) => {
  ...
});

And, it will not work. In fact, it gets a run-time error when executing the router.post() and parsing the path. In diagnosing the issue, it appears to be an old bug in the path-to-regexp library that has long since been fixed, but Express loads the older version of the library that has this problem. So, for now, what you're trying to do appears to require the full regular expression object.

jfriend00
  • 683,504
  • 96
  • 985
  • 979