Express.JS get url path without any additional parameters
Asked Answered
M

3

12

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

Moonlighting answered 11/3, 2017 at 13:15 Comment(1)
Does this answer your question? node.js with express how to remove the query string from the urlBilbao
P
10

I think you can use req.baseUrl + req.path.

And for full URL use req.originalUrl.

Parrisch answered 11/3, 2017 at 16:43 Comment(2)
but if I type /admin I get /admin/ insteadMoonlighting
Edit, I just used replace to trim last trailing slash (req.baseUrl + req.path).replace(/\/$/, "")Moonlighting
G
1

For full url without any parameters, you can use req.protocol + '://' + req.get('host') + req.baseUrl + req.path;

Gony answered 9/11, 2018 at 12:6 Comment(0)
L
0

In frontend you can use window.location.pathname.

Louvre answered 23/4 at 6:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.