when using path-to-regexp, how to match all path that not starting with /api/
?
By using native JavaScript RegExp /^(?!\/api\/).*/
will match/x/y/z
. see test result from here
However, it does not work with path-to-regexp. see the test result from there
So what's the correct way to achieve my goal in path-to-regexp?
[Update 1]
More details: The real case is that I'm using angular2 + koajs. And in angular2, browser may issue a client routing url to server. Please see my another question about this.
To address that issue, as suggested by @mxii, I'm trying to use koa-repath to redirect all requests that not started with /api/
to the root url: http://localhost:3000/
excepte it's static assets (js/json/html/png/...) requests.
And koa-repath
use path-to-regexp
to match the path. That's why I asked this question.
when using path-to-regexp, how to match all path that not starting with /api/
?
By using native JavaScript RegExp /^(?!\/api\/).*/
will match/x/y/z
. see test result from here
However, it does not work with path-to-regexp. see the test result from there
So what's the correct way to achieve my goal in path-to-regexp?
[Update 1]
More details: The real case is that I'm using angular2 + koajs. And in angular2, browser may issue a client routing url to server. Please see my another question about this.
To address that issue, as suggested by @mxii, I'm trying to use koa-repath to redirect all requests that not started with /api/
to the root url: http://localhost:3000/
excepte it's static assets (js/json/html/png/...) requests.
And koa-repath
use path-to-regexp
to match the path. That's why I asked this question.
-
The express route tester is not a regex tester... you enter a route like
/api/user/:name/:age/
and then a path like/api/user/john-doe/21
and it gives you the keys (1. name
and2. route
) and results (name: john-doe
andage: 21
)... the "path-to-regex" module makes a regex out of your route, so you can check via regex if a path string matches it. I don't get what you are trying to achieve, can you please clarify. – Simon Hänisch Commented Aug 24, 2016 at 13:05 - Thanks Simon, please see my update for more details – ricky Commented Aug 25, 2016 at 3:47
3 Answers
Reset to default 4See my ment for an explanation about how the express route tester works.
If you just want a simple regex to check that a string doesn't start with api
or /api
, you don't need "path-to-regex" for that, you can simply use this regex:
/^(?!\/?api).+$/
Explanation:
^ => beginning of string
(?!) => negative look-ahead
\/? => 0 or 1 slash
.+ => 1 or more character (any except newline)
$ => end of string
So you get a match if the start of the string is not followed by /api
(optional slash).
See http://regexr./3e3a6 for an example.
Edit: If you want it the other way round (only match urls starting with /api
or api
, you can use positive look-ahead:
^(?=\/?api).+$
http://regexr./3e3a9
But it's even simpler then:
^\/?api.*$
http://regexr./3e3af
I was trying to do the same thing. It seems like the people who answered didn't actually try to understand what you were asking.
I'm not sure if it's possible to do in path-to-regexp. It seems like it doesn't have negative look ahead.
What you can do is change the order that you route the paths. Add your api routing to express / whatever before you add your static stuff.
app.use('/api', require('./api'));
app.get('/*', function (req, res) {
//serve angular app
});
I did it like this and the api route also works.
Regex required for the pattern blow [/somestring]+,
for example,
- /api - valid
- /asdsa - valid
- / - invalid
- asdsa - invalid
- /asd/asd/asd - valid
- /as/ - invalid
- /aas/a - valid