I need to build a router that sends a REST request to the right controller and action. Here are some examples:
POST /users GET /users/:uid GET /users/search&q=lol GET /users GET /users/:uid/pictures GET /users/:uid/pictures/:pid
It is important to have one regular expression and the best possible, since routing is important and is performed on every request.
we must first replace: (to the end or to the next next slash /) in regular expression URLs, which we can subsequently use to validate the URL with the request URL.
How can we replace these dynamic routes with a regular expression? Like searching for a line that begins with ":" and ends with a "/", a line ends with or "&".
This is what I tried:
var fixedUrl = new RegExp(url.replace(/\\\:[a-zA-Z0-9\_\-]+/g, '([a-zA-Z0-0\-\_]+)'));
For some reason this does not work. How can I implement a regex that replaces: id with a regex or just ignores them compared to the real request URL.
thanks for the help
source share