Quadear Quadear - 4 months ago 9
Node.js Question

Regex Path in node Express

I'm trying to make a route in Node express using regex, but it doesn't work as expected.

The url is :

http://DOMAIN:PORT/reverseproxy/map.google.com/maps?q=lat,lon


The regex i'm using is :

\/reverseproxy\/([^/]*)(.*)


I want to have :

$1 = map.google.com
$2 = /maps?q=lat,lon


as seen here https://regex101.com/r/wN6cZ7/117

But when i do this in express, i have not only 2 params in req.params, but 3:

0 = "aps.google"
1 = ".com/maps"
2 = "com/maps"


I don't understand why it works in the regex tester, but not in my app. Am I missing something ?

Thank you.

Answer

Try this regex:

\/reverseproxy\/(.+[^\/]*)(\/.+)$
Comments