Home > Software engineering >  How to reference a variable in regex URL?
How to reference a variable in regex URL?

Time:11-21

router.get("/(A|B)/account/", async (req, res) => {});

How to do I reference the (A|B) inside of the async function?

CodePudding user response:

I guess your route responsibility is getting account information of only A or B. So let's change your router path to /account/:name(A|B), then your express router will look like this:

router.get("/account/:name(A|B)", async (req, res) => {
    const name = req.params; // A or B
});

Only 2 kinds of requests are handled by this router:

GET /account/A
or
GET /account/B
  • Related