node-muneem / anumargak

Simple but Fastest Web Router in nodejs
Other
39 stars 5 forks source link

Simplify use of regular expressions #3

Closed amitguptagwl closed 6 years ago

amitguptagwl commented 6 years ago

This is how the dynamic route is added currently;

router.on("GET", "/this/is/:dynamic/with/:pattern(\\d+)", handler);

We are planning to name regular expressions used for path parameters

router.addNamedExpression("num","\\d+");
router.on("GET", "/this/is/:dynamic/with/:pattern(:num:)", handler); //valid
//router.on("GET", "/this/is/:dynamic/with/:pattern(\\d+)", handler); //valid

Named expression can be added in following two ways

router.addNamedExpression("num","\\d+");
router.addNamedExpressions({
   "name1" : "regx1",
   "name2" : "regx2",
});

Named expressions will be replaced at the time of adding the route. So it should not impact application performance.

shuklajay117 commented 6 years ago

I'm working on it

amitguptagwl commented 6 years ago

I hope you're clear with the motive behind this issue. Feel free to clear any doubt

shuklajay117 commented 6 years ago

My understanding is as below:

It should support of adding regular expression using addNameExpression method. We will keep track of all regex into a map. Once user have defined some re-usable regex to it, they are able to use it using :regexKeyName: in url. We need to make sure this name should get replaced with specified regex pattern before it gets registered. I assume that there can be multiple dynamic regex keys used in a url so we need to replace all of them.

Please correct me if anything wrong!

amitguptagwl commented 6 years ago

You're right.