1.views/index.html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet"> <style> img { width: 40px; } #login{ float: right; } </style></head><body> <h1>数据展示</h1> <a href="/add" type="button" class="btn btn-success">添加商品</a> <a href="/logout" type="button" class="btn btn-success" id="login">退出登录</a> <table class="table table-bordered table-hover"> <thead> <tr> <th>编号</th> <th>服装名称</th> <th>服装价格</th> <th>服装销量</th> <th>服装图片</th> <th>操作</th> </tr> </thead> <tbody> {{each arr}} <tr> <td>{{$value._id}}</td> <td>{{$value.shopname}}</td> <td>{{$value.price}}</td> <td>{{$value.shopNumber}}</td> <td><img src="{{$value.avatar}}" alt=""></td> <!-- a标签只支持get方式 --> <td><a href="/doDelete?id={{@$value._id}}" type="button" class="btn btn-success">删除</a> <a href="/edit?id={{@$value._id}}" type="button" class="btn btn-warning">修改</a></td> </tr> {{/each}} </tbody> </table></body> </html>
2.routers/index.js
const Router = require("koa-router");const router = new Router();const ShopMangeModel = require("../models/shopMange");// 查询数据库router.get("/",async ctx=>{ var data = await ShopMangeModel.find({}); await ctx.render("index",{arr:data});})module.exports = router;