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

1

3 Answers 3

10

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

And for full URL use req.originalUrl.

Sign up to request clarification or add additional context in comments.

2 Comments

but if I type /admin I get /admin/ instead
Edit, I just used replace to trim last trailing slash (req.baseUrl + req.path).replace(/\/$/, "")
1

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

Comments

0

In frontend you can use window.location.pathname.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.