1. //add.html
    2. <form action="http://localhost:8080/doAdd" method="POST" role="form">
    3. <legend>top250添加数据</legend>
    4. <div class="form-group">
    5. <label for="">电影的名字</label>
    6. <input type="text" class="form-control" id="" name="name" placeholder="输入电影名">
    7. </div>
    8. <div class="form-group">
    9. <label for="">评分</label>
    10. <input type="text" class="form-control" id="" name="rating" placeholder="输入评分">
    11. </div>
    12. <button type="submit" class="btn btn-primary">提交</button>
    13. </form>

    安装依赖

    1. //安装koa-bodyparser这个依赖
    2. yarn add koa-bodyparser

    Tips: input输入框需要加上name属性,不然后台获取不了数据!!!

    1. //index.js 配置bodyparser
    2. ...
    3. const bodyParser = require("koa-bodyparser")
    4. router.post("/doAdd",async ctx=>{
    5. console.log(ctx.request.body);
    6. var {name,rating} = ctx.request.body;
    7. var db = new Top250Model({name,rating});
    8. db.save(err=>{
    9. if(err) throw err;
    10. })
    11. ctx.redirect("/top250")
    12. })
    13. app.use(bodyParser());