关于node.js书写登录注册接口

准备

包准备

  • @escook/express-joi(使用验证规则)
  • joi(验证规则)
  • bcryptjs(密码加密和解密)
  • cors(解决跨域问题)
  • express(配置本地服务器)
  • mysql(导入数据库)

文件夹准备

  • db(装mysql的配置文件)
  • router(路由转接)
  • app.js(总端)
  • router_hanler(路由转接后进行函数处理)
  • schema(进行规则验证)

代码

db

  • index.js
    1
    2
    3
    4
    5
    6
    7
    8
    9
    const mysql = require('mysql')
    const db = mysql.createPool({
    host:'127.0.0.1',
    user:'root',
    password:'123456',
    database:'my_db_01',
    })
    // 向外共享
    module.exports = db

router

  • user.js
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    const express= require('express')

    const router = express.Router()
    // 导入规则验证的中间件
    const expressJoi = require('@escook/express-joi')


    const userHandler = require('../router_handler/user')
    // 导入规则验证文件
    const {reg_login_schema} = require('../schema/user')

    router.post('/login',expressJoi(reg_login_schema),userHandler.loginUser)

    router.post('/reg',expressJoi(reg_login_schema),userHandler.regUser)

    module.exports = router

router_handler

  • user.js
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    const db = require('../db/index')
    const bcyptjs = require('bcryptjs')
    exports.loginUser = (req,res)=>{
    const userinfo = req.body
    const sql = 'select * from ev_users where username=?'
    db.query(sql,userinfo.username,(err,results)=>{
    if(err) return res.cc(err)
    if(results.length !== 1) return res.cc('登录失败',1)
    const compareResult = bcyptjs.compareSync(userinfo.password,results[0].password)
    if(!compareResult) return res.cc('登录失败:密码不正确',0)
    res.send({
    status:0,
    msg:'登录成功'
    })
    })
    }
    exports.regUser = (req,res)=>{
    const userinfo = req.body

    const sql = 'select * from ev_users where username=?'
    db.query(sql,userinfo.username,(err,results)=>{
    if(err) return res.cc(err)
    if(results.length>0) return res.cc('用户名已经被占用')
    // console.log(results.length),如果为1,说明已经有该用户,如果为0,说明没有该用户,可以用于注册
    })
    // 加密密码
    userinfo.password = bcyptjs.hashSync(userinfo.password,10)
    // 执行sql
    const sqlStr = 'insert into ev_users set ?'
    db.query(sqlStr,{username:userinfo.username,password:userinfo.password},(err,results)=>{
    if(err) return res.cc(err)
    if(results.affectedRows !== 1) return res.cc('注册用户失败',1)
    res.cc('注册成功',0)
    })
    }

app.js

  • app.js
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    const express = require('express')

    const app = express()

    const cors = require('cors')
    app.use(express.urlencoded({extended:false}))
    // 简化res.send({})的操作
    app.use((req,res,next)=>{
    res.cc = function(err,status=1){
    res.send({
    status,
    msg:err instanceof Error ? err.msg : err
    })
    }
    next()
    })
    app.use(cors())
    // 导入登录和登录的模块
    const userRouter = require('./router/user')
    app.use('/api',userRouter)
    app.listen(80,()=>{
    console.log('server running at http://127.0.0.1')
    })

schema

  • user.js
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    const joi = require('joi')

    const username = joi.string().alphanum().min(6).max(10).required()

    const password = joi.string().pattern(/^[\S]{6,12}$/).required()

    exports.reg_login_schema = {
    body:{
    username,
    password
    }
    }
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 BieGua
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信