分两步,1.前端发送数据给后端。2.后端接收更改数据库数据
1、前端form方式提交数据
<form action="http://localhost:8080/add" method="POST" role="form"><legend>添加数据</legend><div class="form-group"><label for="">电影名</label><input type="text" class="form-control" id="" name="name" placeholder="输入要提交的电影名字"></div><div class="form-group"><label for="">评分</label><input type="text" class="form-control" id="" name="rating" placeholder="输入要提交的电影的评分"></div><button type="submit" class="btn btn-primary">提交</button></form>
2、后端接收数据并且更改数据库数据
const Top250Model = require("./models/top250");//提交表单router.post("/add",async ctx=>{// console.log(ctx.request.body);var {name,rating} = ctx.request.body; //ctx.request.body是提交的数据var db = new Top250Model({name,rating});db.save(err=>{if(err) throw err;})ctx.redirect("/top250")})
