在注册接口时,我们需要需要获取前端传递过来的数据信息,因此我们需要安装bodyParser这个模块(这篇博客是我koa+MongoDB学习系列中的其中一篇,有一些地方看不懂的可以结合之前发布的博客)。
bodyParser模块的安装
在终端中输入完成安装:
cnpm i koa-bodyparser --save
引入并使用bodyParser
//引入bodyparser
const bodyParser=require('koa-bodyparser');
//使用bodyParser(我使用的位置是test.js)
app.use(bodyParser());
在使用了bodyParser之后,一有 post信息我们就可以通过它获取到ctx.request.body中 ,我们可以写一段测试语句并借助postman来测试一下:
//注册接口
router.post('/register',async ctx =>{
console.log(ctx.request.body);
});