Search code examples
node.jsexpressexpress-handlebars

express-handlebars: Linking to a handlebar using <a> tag


I have created 2 handlebars files in 'Views' folder:

  1. index.handlebars
  2. contact.handlebars

I want to add a link to contact.handlebars in index.handlebars.

I tried using 'a' tag:

<a href="views/contact.handlebars">Contact Page</a>
<a href="contact.handlebars">Contact Page</a>
<a href="contact.html">Contact Page</a>

but none of them is redirecting it to the contact.handlebars page.


Solution

  • Since the browser cannot interpret a handblebars template, you need to link to the route in your server from which contact.handlebars is being rendered.

    <a href='/contact'>Contact</a>

    This is if your route on your server is at /contact for the contact page

    Your express server should be serving two routes

    app.get('/', (req, res) => {
        // render your index.handblars
    })
    
    app.get('/contact', (req, res) => {
        // render your contact.handlebars
    })