Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not works when use regular exp or set "/" to exclude path. #3404

Closed
sinbargit opened this issue Aug 31, 2017 · 1 comment
Closed

not works when use regular exp or set "/" to exclude path. #3404

sinbargit opened this issue Aug 31, 2017 · 1 comment

Comments

@sinbargit
Copy link

Express version:4.15.4 ,nodeJs version:6.11

. 1 look the following code. I use a regular exp to match a path and route the request to pageRouter.When I request the localhost:8000/new, the regular EXP looks not work, but I have test the EXP use /^/(?!api$|api/.|resource$|resource/.)/.test('/new'), it return true. and if I use /(?!api$|api/.|resource$|resource/.)/ it works but that against with the api doc.

router.use('/api',apiRouter);
router.use('/resource',resourceRouter);
router.use(/^\/(?!api$|api\/.*|resource$|resource\/.*)/,pageRouter);

pageRouter.get("*",(req,resp,next)=>{let name = req.originalUrl...})

. 2 look the following code. The request first through the resourceRouter then through pageRouter but I think that should be just through the resourceRouter .

router.use('/api',apiRouter);
router.use('/resource',resourceRouter);
router.use("/",pageRouter);

pageRouter.get("*",(req,resp,next)=>{let name = req.originalUrl...})
@dougwilson
Copy link
Contributor

HI @sinbargit the issue with your regular expression is how .use works. It is not just a matter of does the regular expression match the input, but since .use needs to strip down the matched path, it expects your match to stop before a / character to cut on.

$ node -pe "/^\/(?!api$|api\/.|resource$|resource\/.)/.exec('/test')"
[ '/', index: 0, input: '/test' ]

The regular expression on your URL is matching one character, which makes the cut-off point the character t, which is not a / character. If you want to continue down the regular expression path, the easiest solution is to just adjust the regular expression to match just the part you want to cut off prior to entering the pageRouter. If you want to cut nothing (i.e. enter the pageRouter with the full URL), then use /^(?!\/api$|\/api\/.|\/resource$|\/resource\/.)/.

I hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants