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.

Share:
10,826
Jakub Pastuszuk
Author by

Jakub Pastuszuk

Updated on June 22, 2022

Comments

  • Jakub Pastuszuk
    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
    Jakub Pastuszuk over 6 years
    but if I type /admin I get /admin/ instead
  • Jakub Pastuszuk
    Jakub Pastuszuk over 6 years
    Edit, I just used replace to trim last trailing slash (req.baseUrl + req.path).replace(/\/$/, "")