Express.JS get url path without any additional parameters
10,826
I think you can use req.baseUrl + req.path
.
And for full URL use req.originalUrl.
Author by
Jakub Pastuszuk
Updated on June 22, 2022Comments
-
Jakub Pastuszuk over 1 year
Is there any straightforward way to get URL path segment without any additional data (url parameters, css '#', etc.)
for example:
full URL:http://example.com/panel/list
path segment I want to get:/panel/list
full URL:http://example.com/panel/list?user=joe
path segment I want to get:/panel/list
full URL:http://example.com/panel/list#header
path segment I want to get:/panel/list
And any other combination of data that is not path segment of my URL
-
Jakub Pastuszuk over 6 yearsbut if I type /admin I get /admin/ instead
-
Jakub Pastuszuk over 6 yearsEdit, I just used replace to trim last trailing slash (req.baseUrl + req.path).replace(/\/$/, "")