Express 如何使用ejs显示todo

Express 如何使用ejs显示todo,express,ejs,Express,Ejs,我正在玩ejs,想知道如何在html中显示一些TODO 这就是我必须处理的问题: app.get("/", (req, res) => { db.collection("todoejs") .find() .toArray((err, todoejs) => { res.render("todo"); }); }); app.post("/create-item", (req, res) => {

我正在玩ejs,想知道如何在html中显示一些TODO

这就是我必须处理的问题:

app.get("/", (req, res) => {
    db.collection("todoejs")
        .find()
        .toArray((err, todoejs) => {
            res.render("todo");
        });
});

app.post("/create-item", (req, res) => {
    console.log(req.body);
    db.collection("todoejs").insertOne({todo: req.body.item});
    res.redirect("/");
});
我的视图文件夹中有一个todo.ejs,我想知道如何显示todo

这是ejs文件中的内容:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Simple To-Do App</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
</head>
<body>
  <div class="container">
    <h1 class="display-4 text-center py-1">To-Do App</h1>

    <div class="jumbotron p-3 shadow-sm">
      <form action="create-item" method="POST">
        <div class="d-flex align-items-center">
          <input name="item" autofocus autocomplete="off" class="form-control mr-3" type="text" style="flex: 1;">
          <button class="btn btn-primary">Add New Item</button>
        </div>
      </form>
    </div>

    <ul class="list-group pb-5">
      <li class="list-group-item list-group-item-action d-flex align-items-center justify-content-between">
        <span class="item-text"><%= todoejs %></span>
        <div>
          <button class="edit-me btn btn-secondary btn-sm mr-1">Edit</button>
          <button class="delete-me btn btn-danger btn-sm">Delete</button>
        </div>
      </li>
    </ul>

  </div>

</body>
</html>

简易应用程序
待办应用
添加新项目
  • 编辑 删除

res.render的第二个参数接受要传递给模板的变量

在本例中,您可能需要执行
res.render(“todo”,{todoejs:todoejs})

然后在模板中定义
todoejs
,您可以随意使用它