Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link href in a pug template

I am using pug as the view engine of a nodejs application. I have a layout that every other current view extends, that contains a navbar with links to common urls across the app.

For example, a link to the signin url would look like :

a(href='/auth/signin')`

This works fine from the root url ('/'), correctly leads to '/auth/signin'.

Within the '/auth' module which contains the routes for '/auth/signin', '/auth/signup' and '/auth/signout', the behavior is different. Instead, the route is concatenated with the current module's name. So for example, within the '/auth/signin' route, the link is actually a link to '/auth/auth/signin'. Clicking on it naturally leads to a 404, but on that page the link to signin is a link to '/auth/auth/auth/signin'.

And so on and so forth.

I don't fully understand what is going on here and how to prevent it. Is there away to link to my routes in absolute terms in pug without straight up typing the full url (which is unpractical for a variety of reasons), the same way you'd use a route helper in Ruby on Rails ?

like image 827
Nano Avatar asked Feb 28 '26 09:02

Nano


1 Answers

Solution from the comments:

If you start your href's with a slash then these are interpreted as absolute url's. Then it does not matter in which folder your pug file is located. Please check that your href's start with / always.

like image 187
Graham Avatar answered Mar 03 '26 02:03

Graham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!