1.views/index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
  9. <style>
  10. img {
  11. width: 40px;
  12. }
  13. #login{
  14. float: right;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <h1>数据展示</h1>
  20. <a href="/add" type="button" class="btn btn-success">添加商品</a>
  21. <a href="/logout" type="button" class="btn btn-success" id="login">退出登录</a>
  22. <table class="table table-bordered table-hover">
  23. <thead>
  24. <tr>
  25. <th>编号</th>
  26. <th>服装名称</th>
  27. <th>服装价格</th>
  28. <th>服装销量</th>
  29. <th>服装图片</th>
  30. <th>操作</th>
  31. </tr>
  32. </thead>
  33. <tbody>
  34. {{each arr}}
  35. <tr>
  36. <td>{{$value._id}}</td>
  37. <td>{{$value.shopname}}</td>
  38. <td>{{$value.price}}</td>
  39. <td>{{$value.shopNumber}}</td>
  40. <td><img src="{{$value.avatar}}" alt=""></td>
  41. <!-- a标签只支持get方式 -->
  42. <td><a href="/doDelete?id={{@$value._id}}" type="button" class="btn btn-success">删除</a>
  43. <a href="/edit?id={{@$value._id}}" type="button" class="btn btn-warning">修改</a></td>
  44. </tr>
  45. {{/each}}
  46. </tbody>
  47. </table>
  48. </body>
  49. </html>

2.routers/index.js

  1. const Router = require("koa-router");
  2. const router = new Router();
  3. const ShopMangeModel = require("../models/shopMange");
  4. // 查询数据库
  5. router.get("/",async ctx=>{
  6. var data = await ShopMangeModel.find({});
  7. await ctx.render("index",{arr:data});
  8. })
  9. module.exports = router;