Exclude words from expressjs route path

Is there a way to make it not match /apiand /assets?

router.use("/:group", groupRouter);

I tried the following but it did not work.

router.use("/:group(!(api|assets))", groupRouter);

Also, I tried using a regex here, but node gave me an error stating that it was expecting a callback, not a regex.

Note: apparently it .usedoesn’t record group, but this is not necessary in my case. I just need it to match all but a few words.

+4
source share
1 answer

I used the embedded middleware to solve this problem.

router.use("/:group", function(req, res, next) {
  var excludes = ["api", "assets"];
  if (excludes.indexOf(req.params.group) !== -1) return next();
  else {
    router.use("/"+req.params.group, groupRouter);
    next();
  }
});
+4
source

Source: https://habr.com/ru/post/1547224/


All Articles