NodeJS 如何在ejs模板中使用循环显示多个结果?

ctehm74n  于 11个月前  发布在  Node.js
关注(0)|答案(1)|浏览(125)

截图:(https://i.stack.imgur.com/w8z5M.png
我的代码:(ejs文件代码)

<div class="container">
      <h1 class="text-center">Domain Age Checker</h1>``
      <form action="/domainagechecker" method="post">
        <div class="form-group">
          <label for="domain">Domain URL:</label>
          <textarea
            class="form-control"
            type="text"
            name="domain"
            id=""
            placeholder="write a domain"
          ></textarea>
        </div>
        <br />
        <div class="form-group">
          <button class="btn btn-block btn-primary">Get Details</button>
        </div>
      </form>
      <br />

      <%if (flag) { %>

      <table class="table table-striped">
        <thead>
          <tr>
            <th>Domain Name</th>
            <th>Running Server</th>
            <th>Creation Date</th>
            <th>Domain Age</th>
            <th>Last Renewal</th>
            <th>Expiry Date</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td><%=data.domainName%></td>
            <td><%=data.registrar%></td>
            <td><%=date%></td>
            <td><%=domainAge%></td>
            <td><%=data.updatedDate%></td>
            <td><%=data.registryExpiryDate%></td>
          </tr>
        </tbody>
      </table>
   <% } %>
</div>

字符串

  • (node js apis:)
app.set('view engine', 'ejs');

app.get("/", (req,res) => {
    res.render("domainagechecker", {
      title: "Domain Details",
      data: "",
      flag: false,
      date: "",
      domainAge: "",
    });
})

app.post("/domainagechecker", async (req, res) => {
  var domain = req.body.domain;
  console.log(domain);

  var list_Domains = domain.split(" ");

  for (let i = 0; i < list_Domains.length; i++) {
    console.log("sl.no " + i + " " + list_Domains[i]);

    if (isValidDomain(list_Domains[i])) {
      var result = await whoisinfo(list_Domains[i]);
      console.log(result);

      var date = moment(result.creationDate).format("YYYY-MM-DD");
      var curDate = moment(new Date()).format("YYYY-MM-DD");

      console.log("creation date: " + date);
      console.log(curDate);

      var a = moment(date);
      var b = moment(curDate);
      console.log(a);
      console.log(b);

      var years = b.diff(a, "years");
      a.add(years, "years");

      var months = b.diff(a, "months");
      a.add(months, "months");

      var days = b.diff(a, "days");

      var domainAge =
        years + " years " + months + " months and " + days + " days";

      res.render("domainagechecker", {
        title: "Domain Details",
        data: result,
        flag: true,
        date: date,
        domainAge: domainAge,
        domain1: domain,
      });
    } else {
      res.send("please enter domain name as 'example.com' (only domain name)");
    }
  }
});


我已经尝试了for循环,但它对我不起作用。请告诉我解决方案。
API正在根据输入获取结果,但ejs模板仅显示第一个值。

1yjd4xko

1yjd4xko1#

你是一个接一个地渲染,一次得到结果,而不是把所有的数据放在数组中,那么你将得到所有的数据

app.get("/", (req, res) => {
    res.render("domainagechecker", {
        title: "Domain Details",
        results: [],
        flag: false,
    });
});

字符串
然后推送所有数据:

results.push({
  domainName: list_Domains[i],
  data: result,
  date: date,
  domainAge: domainAge,
});


你的最终ejs应该在results Array中呈现完整的数据:

<table class="table table-striped">
  <thead>
    <tr>
      <th>Domain Name</th>
      <th>Running Server</th>
      <th>Creation Date</th>
      <th>Domain Age</th>
      <th>Last Renewal</th>
      <th>Expiry Date</th>
    </tr>
  </thead>
  <tbody>
    <% results.forEach(function(result) { %>
    <tr>
      <td><%= result.domainName %></td>
      <td><%= result.data.registrar %></td>
      <td><%= result.date %></td>
      <td><%= result.domainAge %></td>
      <td><%= result.data.updatedDate %></td>
      <td><%= result.data.registryExpiryDate %></td>
    </tr>
    <% }); %>
  </tbody>
</table>


nodejs:

app.get('/', (req, res) => {
  res.render('domainagechecker', {
    title: 'Domain Details',
    results: [],
    flag: false,
  });
});

app.post('/domainagechecker', async (req, res) => {
  var domain = req.body.domain;
  console.log(domain);

  var list_Domains = domain.split(' ');

  var results = [];

  for (let i = 0; i < list_Domains.length; i++) {
    console.log('sl.no ' + i + ' ' + list_Domains[i]);

    if (isValidDomain(list_Domains[i])) {
      var result = await whoisinfo(list_Domains[i]);
      console.log(result);

      var date = moment(result.creationDate).format('YYYY-MM-DD');
      var curDate = moment(new Date()).format('YYYY-MM-DD');

      console.log('creation date: ' + date);
      console.log(curDate);

      var a = moment(date);
      var b = moment(curDate);
      console.log(a);
      console.log(b);

      var years = b.diff(a, 'years');
      a.add(years, 'years');

      var months = b.diff(a, 'months');
      a.add(months, 'months');

      var days = b.diff(a, 'days');

      var domainAge = years + ' years ' + months + ' months and ' + days + ' days';

      results.push({
        domainName: list_Domains[i],
        data: result,
        date: date,
        domainAge: domainAge,
      });
    } else {
      res.send("please enter domain name as 'example.com' (only domain name)");
      return; // Stop execution if validation fails
    }
  }

  res.render('domainagechecker', {
    title: 'Domain Details',
    results: results,
    flag: true,
  });
});

相关问题