邮箱验证注册接口 & apidoc生成api文档
要求:邮箱验证注册逻辑接口实现
a. 验证用户名存在
b. 获取邮箱验证码
1. 获取邮箱验证码接口 a.发送邮件 b.邮箱和验证码保存到内存中
2. 5分钟之内 不能重复发送
{1111@qq.com:{ ctime:第一次发送的时间戳,code:1233}}
3. 5分钟之内 发送次数不能超过三次
{1111@qq.com:{ ctime:第一次发送的时间戳,code:1233,count:1}}
⚠️注意: 发送验证码后,要把验证码保存在内存中,是为了注册的时候能再次使用。
const express=require('express')
const router= express.Router()
const User=require('../db/model/userModel')
const Mail=require('../utils/mail')
let codes={} //通过内存保存验证码
/**
* @api {post} /user/reg 用户注册
* @apiName 用户注册
* @apiGroup User
*
* @apiParam {String} us 用户名.
* @apiParam {String} ps 用户密码.
* @apiParam {String} code 验证码.
* @apiSuccess {String} firstname Firstname of the User.
* @apiSuccess {String} lastname Lastname of the User.
*/
router.post('/reg',(req,res)=>{
// 获取数据
let {us,ps,code}=req.body
// if(!us || !ps) {return res.send({err:-1,msg:'参数错误'})}
if(us&&ps&&code){
//判断验证码是否ok
console.log(codes[us])
console.log(code)
console.log(codes)
if(codes[us]!=code){return res.send({err:-4,msg : '验证码错误'})}
User.find({us})
.then((data)=>{
if(data.length===0){
// 用户名不存在 可以注册
return User.insertMany({us:us,ps:ps})
}else{
res.send({err:-3,msg:'用户名已存在'})
}
})
.then(()=>{
res.send({err:0,msg:'注册ok'})
})
.catch((err)=>{
res.send({err:-2,msg:'注册err'})
})
}else{
return res.send({err:-1,msg:'参数错误'})
}
console.log(us,ps)
// 数据处理
// 返回数据
})
/**
* @api {post} /user/login 用户登录
* @apiName login
* @apiGroup User
*
* @apiParam {String} us 用户名.
* @apiParam {String} ps 用户密码.
* @apiSuccess {String} firstname Firstname of the User.
* @apiSuccess {String} lastname Lastname of the User.
*/
router.post('/login',(req,res)=>{
let {us,ps}=req.body
if(!us||!ps ){ return res.send({err:-1,msg : '参数错误'})}
// {us:us,ps:ps} === {us,ps}
User.find({us,ps})
.then((data)=>{
if(data.length>0){
res.send({err:0,msg : '登录ok'})
}else{
res.send({err:-2,msg : '用户名或密码不正确'})
}
console.log(data)
})
.catch((err)=>{
return res.send({err:-1,msg : '内部错误'})
})
})
/**
* @api {post} /user/getMailCode 发送邮箱验证码
* @apiName 发送邮箱验证码
* @apiGroup User
*
* @apiParam {String} mail 邮箱.
* @apiSuccess {String} firstname Firstname of the User.
* @apiSuccess {String} lastname Lastname of the User.
*/
// 发送邮件验证码
router.post('/getMailCode',(req,res)=>{
console.log(req.body)
let {mail}=req.body //这里也要判断参数是否存在,不存在的话抛出错误
let code=parseInt(Math.random()*10000)// 产生随机码
console.log(codes)
Mail.send(mail,code)
.then(()=>{
codes[mail]=code
//将邮箱和邮箱匹配的验证码保存到缓存中】
res.send({err:0,msg:'验证码发送ok'})
})
.catch((err)=>{
res.send({err:-1,msg:'验证码发送no ok'})
})
})
module.exports=router
补充扩展:手机验证码注册,不同之处就是 封装的模块(SendMail)不也一样,有不同的运营商,调用运营商提供的接口就可以。
sendMail模块👇

apiDoc 插件:通过注释自动生成api文档 :https://www.npmjs.com/package/apidoc
三步骤:安装,配置,按照规范写注释。
安装: npm install -g apidoc
运行: apidoc -i src/ -o doc/
/** * @api {get} /user/:id Request User information * @apiName GetUser * @apiGroup User * * @apiParam {Number} id User's unique ID. * * @apiSuccess {String} firstname Firstname of the User. * @apiSuccess {String} lastname Lastname of the User. */
apidoc.json文件:写公共信息
{ "name": "这是个接口文档", "version": "0.1.0", "description": "use注册的接口文档(注册,登录,发送验证码)", "title": "你好呀", "url" : "http://127.0.0.1:3000" }
具体示例可以看上边接口。

浙公网安备 33010602011771号