I have a problem with my application made in Express 4.x, I have an authentication Middleware checking for the route called and I would like to get the route without parameters ...
Example :
I declared this route : /authentication/newPassword/:token
So I would like to know if the request.path matches this particular route...
This is my url : /authenticate/newPassword/XRlIjoiMjAxOC0wOC0xM1QwOTNzo1Mi43ODBaIn0=
So I was looking in differents variables :
req.path // /authenticate/newPassword/XRlIjoiMjAxOC0wOC0xM1QwOTNzo1Mi43ODBaIn0=
req.originalUrl // /authenticate/newPassword/XRlIjoiMjAxOC0wOC0xM1QwOTNzo1Mi43ODBaIn0=
req.url // /authenticate/newPassword/XRlIjoiMjAxOC0wOC0xM1QwOTNzo1Mi43ODBaIn0=
req.pathname // undefined
I was looking on google and I saw someone answering the use of req.headers, with the .refers to know the path, but my express aplication is an API and it's called by other Applications on other URL so referrers don't match the good url
I found a solution to my problem using regexp like this :
const pathOk = /\/authenticate\/newPassword\/.+/;
if(pathOk.test(req.path)) { //Continue the middleware ...
So my question is easy, is there something in Express or Node to make something like that :
if (req.{something like path} == '/authentication/newPassword/:token') {
Thanks for your answers.
Express uses path-to-regexp for matching the route paths.
Using this module, you can automatically create a regular expression from your route string.
var pathToRegexp = require('path-to-regexp')
var re = pathToRegexp('/authentication/newPassword/:token')
if(re.test(req.path)) { //...
Or in a one liner:
if (require('path-to-regexp')('/authentication/newPassword/:token').test(req.path) { //...