Search code examples
node.jsexpresshandlebars.js

How to assign a route present in index.js file from hbs?


I want to refer my signout menu item which is defined under views folder as myprofile.hbs to a route /logout defined inside index.js

I tried pasting the path into a href= of that file.

//Present inside views/myprofile.hbs

<li>
<a href="./index.js/logout">
<i class="glyphicon glyphicon-log-out" id="signout"></i>
Sign Out </a>
</li>

//present inside index.js

app.get("/logout",function(req,res){
    req.session.destroy();
    res.redirect("/");
});

Solution

  • Assuming that the above app.get() is at the root of your application, that href just needs to be /logout.

    <a href="/logout">
    

    matching your path in Express:

    app.get("/logout"), . . .