Shiqi Zhou
Shiqi Zhou

Reputation: 23

Passing link text from a hyperlink to another page. (Express JS)

I am facing a challenge in my code where I want to pass a link text in a hyperlink to another page. My web app is build based on Express.

This is the hyperlink (in a dropdown list) in one page:

<a class="dropdown-item" href="/history" ><%=dateArray[index-1]%></a>

I want to grab the link text dateArray[index-1], and send it to /history page.

This is relative code in my app.js:

app.route("/history")
.get(function(req, res) {
  getDate();
  

  List.find({}, function(err, lists) {
    res.render("history", {
      day: day,
      lists: lists
    });
  });
})

Please ignore the irrelevant code here. What should I do here in app.js to grab the dateArray[index-1] value and send it to /history route?

Million thanks in advance.

Upvotes: 2

Views: 562

Answers (1)

Niloy
Niloy

Reputation: 606

You can use query string or req.param of express to do that

Req param (easy and better):

<a class="dropdown-item" href="/history/<%=dateArray[index-1]%>" ><%=dateArray[index-1]%></a>

In Route

app.route("/history/:data")
.get(function(req, res) {
    var urlData = req.params.data;
    console.log(urlData);
})

Hope this helps

Upvotes: 2

Related Questions