从 HTML 获取动态路由输入

Getting dynamic routing input from HTML

我正在使用 ejs 作为模板语言。

<main class="content">
    <% documents.forEach(function(documentObject) { %>
    <h1><a href="/showprofile/:username"><%= documentObject.username %></a> solved </h1>
    <h2><%= documentObject.problem_id %>. <%= documentObject._statement %> in  
    <%= documentObject.time %>  seconds on  
    <%= documentObject.date_added %> . </h2>
    <% }) %>


</main>

我正在使用动态路由 /showprofile/:username 进行重定向。我需要以这样的方式制作超链接,使得 documentObject.username 的值作为参数传递给 :username 。我该怎么做?

您只需使用 ejs 函数在您的 href 属性中呈现用户名而不是 :username

<h1><a href="/showprofile/<%= documentObject.username %>"><%= documentObject.username %></a> solved </h1>