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?

5
  • Did you try it? What did it match? Per the last example in this article, yours looks like it would work. Commented Oct 30, 2021 at 4:40
  • Is the ^ necessary, though? Commented Oct 30, 2021 at 4:46
  • See stackoverflow.com/questions/10858005/… for some examples. Commented Oct 30, 2021 at 4: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. Commented Oct 30, 2021 at 5:04
  • See examples in the Express doc here: 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. Commented Oct 30, 2021 at 5:22

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.