1.安装模块

npm install koa-bodyparser --save

npm install bcryptjs --save

2.引入模块

根目录/app.js

const bodyParser = require('koa-bodyparser');

// 实例化koa
const app = new Koa(); app.use(bodyParser());

3.注册接口

根目录/routes/api/users.js

const Router = require('koa-router');
const router = new Router();
const bcrypt = require('bcryptjs'); // 引入User
const User = require('../../models/User'); /**
* @route GET api/users/test
* @desc 测试接口地址
* @access 接口是公开的
*/
router.get('/test', async ctx => {
ctx.status = 200;
ctx.body = { msg: 'users works...' };
}); /**
* @route POST api/users/register
* @desc 注册接口地址
* @access 接口是公开的
*/
router.post('/register', async ctx => {
// console.log(ctx.request.body); // 通过邮箱判读是否注册过
const findResult = await User.find({ email: ctx.request.body.email });
// console.log(findResult);
if (findResult.length > 0) {
ctx.status = 500;
ctx.body = { email: '邮箱已被占用 ' };
} else {
// 没查到
const newUser = new User({
name: ctx.request.body.name,
email: ctx.request.body.email,
password: ctx.request.body.password
}); await bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
// console.log(hash);
if (err) throw err;
newUser.password = hash;
})
}) // console.log(newUser);
// 存储到数据库
await newUser.save().then(user => {
ctx.body = user;
}).catch(err => {
console.log(err);
}); // 返回json数据
ctx.body = newUser;
}
}); module.exports = router.routes();

4.测试

.

koa 项目实战(四)注册接口和调试工具(postman)的更多相关文章

  1. miniFTP项目实战四

    项目简介: 在Linux环境下用C语言开发的Vsftpd的简化版本,拥有部分Vsftpd功能和相同的FTP协议,系统的主要架构采用多进程模型,每当有一个新的客户连接到达,主进程就会派生出一个ftp服务 ...

  2. koa 项目实战(十一)验证登录和注册的 input

    1.验证注册参数 根目录/validation/register.js const Validator = require('validator'); const isEmpty = require( ...

  3. koa 项目实战(七)登录接口

    1.登录接口 /** * @route POST api/users/login * @desc 登录接口地址 * @access 接口是公开的 */ router.post('/login', as ...

  4. koa 项目实战(九)passport验证token

    1.安装模块 npm install koa-passport -D npm install passport-jwt -D 2.解析token 根目录/config/passport.js cons ...

  5. koa 项目实战(八)生成token

    1.安装模块 npm install jsonwebtoken --save 2.引用 const jwt = require('jsonwebtoken'); ... // 返回token cons ...

  6. koa 项目实战(六)注册接口加密

    1.创建工具类 根目录/config/tools.js const bcrypt = require('bcryptjs'); const tools = { enbcrypt(password) { ...

  7. koa 项目实战(三)创建测试接口和用户模型

    1.创建测试接口,并引入用户模型 根目录/routes/api/users.js const Router = require('koa-router'); const router = new Ro ...

  8. Android项目实战登录&注册

    由于项目中大部分界面都有一个后退键和一个标题栏,为避免代码冗杂以及便于利用,我们可以将后推荐和标题栏单独抽取出来定义一个标题栏布局,在 res/layout 目录下新建一个 Layout resour ...

  9. Android项目实战登录&注册

    由于项目中大部分界面都有一个后退键和一个标题栏,为避免代码冗杂以及便于利用,我们可以将后推荐和标题栏单独抽取出来定义一个标题栏布局,在 res/layout 目录下新建一个 Layout resour ...

随机推荐

  1. element table切换分页不勾选的自带方法

    场景一:没有回显勾选的情况 table表格加row-key标识选中行唯一标识,多选框加reserve-selection设置为true <template> <el-table v- ...

  2. 【Day2】3.面向对象编程

    课程目标 1. 面向对象编程 2. 类和实例 3. 访问限制 4. 实例属性和类属性 面向对象编程 • 面向对象编程是一种程序设计思想 • 面向对象把类和对象作为程序的基本单元 • 对象包含属性和方法 ...

  3. Win7自带的系统备份还原功能如何去使用?

    很多用户都会反映Win7系统使用过程中会出现系统或应用程序方面的小故障,针对这些小问题,再选择进行电脑系统的重装就有些过于麻烦了. 其实Win7系统内带有系统备份和还原的功能,可以在电脑系统出现小问题 ...

  4. FASTCGI/CGI

    在了解这两个协议之前,我们先谈一下动态网页 动态网页 是指跟静态网页相对的一种网页编程技术.静态网页,随着html代码的生成,页面的内容和显示效果就基本上不会发生变化了--除非你修改页面代码.而动态网 ...

  5. xenserver 下载模板

    cd /tmp rm -rf Auto.sh wget http://os.xensystem.net/XenSystem/download/Customer/Auto.sh sh Auto.sh

  6. Sass的混合-@mixin,@include

    1,无参数,有参数和带默认值参数的@mixin声明sass文件内容: //带参数,默认50@mixin opa($opa:50){ opacity: $opa / 100; filter:alpha( ...

  7. Selenium(5)

    一.WebDriver结合Junit的使用 1.Junit中常用的断言 (1)assertEquals:断言实际结果与预期结果是否相等 Equals:相等 格式:assertEquals(预期值,实际 ...

  8. yii\base\InvalidCallException The cookie collection is read only.

    Invalid Call – yii\base\InvalidCallException The cookie collection is read only. 在使用Yii2进行cookie操作时会 ...

  9. error LNK2019 : unresolved external symbol Zbar配置问题

    原文链接:https://blog.csdn.net/MengchiCMC/article/details/77871714 出现error LNK2019 : unresolved external ...

  10. .net core 读取appsettings 的配置

    { "Logging": { "IncludeScopes": false, "LogLevel": { "Default&quo ...