Search code examples
node.jsexpresspug

a href loses relative directory in Jade in Express


My Express web app has a Jade view which contains a element.

The current URL is http://localhost:3000/location. This page has a list of locations, each of which is represented by a ID in a hyperlink, e.g.

http://localhost:3000/location/1
http://localhost:3000/location/4
http://localhost:3000/location/202

In the location.jade, my code is

ul#locList
    for location in jsonData
      li
        a(href='#{location.locationid}')
          h2 #{location.locationname} (#{location.locationid})

The generated hyperlink is http://localhost:3000/1.

I don't want to hardcode "location/" in the view. How can I get the relative directory in the href?


Solution

  • You can get the full url path from server side and send it the view using the respective route req.protocol + '://' + req.get('host') + req.originalUrl