1-1 加载页面
在controller 文件夹 新建 home.js
'use strict';
const Controller = require('egg').Controller;
class HomeController extends Controller {
async index() {
const { ctx } = this;
var db = this.app.mongodb;
const collection = await db.collection('member');
//拿到member中所有数据
var members = await collection.find().toArray();
console.log(members);
await ctx.render("/home",{members});
}
}
module.exports = HomeController;
在view 文件夹 新建 home.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>
<style>
.logo {
width: 50px;
}
</style>
<meta name="referrer" content="no-referrer" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th>id</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>头像</th>
<th>删除</th>
</tr>
</thead>
<tbody>
{%for item in members%}
<tr>
<td>{{item._id}}</td>
<td><a href="/detail?_id={{item._id}}">{{item.username}}</a></td>
<td>{{item.age}}</td>
<td>{{item.sex}}</td>
<td><img class="logo" src="{{item.logo}}" alt=""></td>
<td><a href="/doDelete?_id={{item._id}}" class="btn btn-danger">删除</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>
对应路由
router.get('/form', controller.form.index);