Mohit Kumar Sharma
Mohit Kumar Sharma

Reputation: 667

Navigating to another page dynamically using href using node js and express ejs

I am creating a sample blog website in which homepage contains blogs in order of the day it is posted. The layout of the blog is like below :

Day 1

some paragraph text.....Read More

Day 2

some paragraph text.....Read More

Now how do route/redirect the page to particular post page using Day as Id and using anchor tag when clicking Read More

Upvotes: 0

Views: 1121

Answers (1)

Darshan J
Darshan J

Reputation: 239

EJS Code:

...
<!--
day=[
 {
  id:1,
  ...any_other_data
 },
 {
  id:2,
  ...any_other_data
 }
]
-->
<% for (var i = 0; i < day.length; i++) { %>
    <a href="<%= 'example/'+day[i].id %>">Read More</a>
    <a href="<%= 'https:www.example.com/example/'+day[i].id %>">Read More</a>
<%}%>
...

I hope this should work! In the above code the dynamic url will work for both the codes

Dynamic Routing with Express.js

app.get('example/:id', function(req , res){
  res.render('Day' + req.params.id);
});

Upvotes: 1

Related Questions