• node写登录


    "dependencies": {
        "body-parser": "^1.20.2",
        "cors": "^2.8.5",
        "express": "^4.18.2",
        "jsonwebtoken": "^9.0.0",
        "md5": "^2.3.0",
        "mysql": "^2.18.1"
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    const express = require('express')
    const app = express()
    app.listen(3001,()=>{
        console.log('serve is running at http://127.0.0.1:3001')
    })
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    创建数据库连接

     let db = mysql.createConnection({
         host:'127.0.0.1',
         user:'root',
         password:'123456',
         database:'books',
         port:3306
     })
    
     db.connect((err)=>{
         if(err) throw err;
         console.log('连接成功')    
     })
    
     setInterval(function(){
         db.query('select 1')
     },5000);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    加粗样式

    // 创建连接池
    const pool = mysql.createPool({
        host: '127.0.0.1',
        user: 'root',
        password: '123456',
        database: 'books',
        connectionLimit: 10 // 设置连接池最大连接数
      });
      
      // 查询函数
      function query(sql, callback) {
        // 从连接池中获取一个连接
        pool.getConnection((err, connection) => {
          if (err) {
            callback(err, null);
          } else {
            // 执行查询
            connection.query(sql, (err, results) => {
              // 释放连接
              connection.release();
              callback(err, results);
            });
          }
        });
      }
      
      // 定时任务
      setInterval(() => {
        const sql = 'SELECT 1';
        query(sql, (err, results) => {
          if (err) {
            console.error(err);
          } else {
            // console.log('查询结果:', results);
          }
        });
      }, 5000);
    
    
    • 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
    • 36
    • 37
    • 38

    在上面的代码中两种写法我们都设置了一个定时任务,这段代码是为了保持数据库连接处于活动状态,每隔5秒会向数据库发送一个请求,确保数据库连接不会因为长时间没有交互而被断开,这个技巧被称为“保活”
    配置跨域请求和解析前端数据
    以下配置表示允许所有的网址和方法请求

    app.use((req, res, next) => {               //解决跨域问题,能够允许所有网址和方法的跨域处理
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
        res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
        if ('OPTIONS' === req.method) {
          res.sendStatus(200);
        } else {
          next();
        }
      });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    配置解析前端传递的数据用到了body-parser这个包

    const bodyParser = require('body-parser');
    app.use(bodyParser.json())              //解析json
    app.use(bodyParser.urlencoded({ extended: true }));             //解析客户端传递过来的参数 
    
    
    • 1
    • 2
    • 3
    • 4

    登录接口实现
    登录接口实现思路
    首先接收前端请求的参数,从中解析出用户名和密码,先判断用户名和密码是否存在,不存在直接返回
    如果用户名密码都存在,对密码进行md5加密,在进行数据库查询,没有找到返回用户名或密码错误
    查询到数据说明,用户名密码正确生成token返回给客户端

    // 登录接口
    app.post('/login', (req, res) => {
        const { username, password } = req.body;
    
        if(!username || !password) res.json({ code:403,message: '用户名或密码不能为空' });
        // 进行MD5加密
        const md5Pwd = md5(password);
        // 查询数据库中是否存在该用户
        const sql = `SELECT * FROM user WHERE username='${username}' AND password='${md5Pwd}'`;
        query(sql, (err, result) => {
            if (err) throw err;
            if (result.length === 0) {
                res.json({
                    code: 1,
                    message: '用户名或密码错误'
                });
            } else {
                // 验证成功,生成token并返回
                const payload = {username: username}; // 按照需求设置payload
                const secretKey = '147258'; // 按照需求设置密钥
                const token = jwt.sign(payload, secretKey, {expiresIn: '1h'}); // 生成token,设置过期时间1小时
    
                // 将token返回给客户端
                res.json({
                    token:token,
                    code: 0,
                    message: '登录成功'
                });
            }
        });
    });
    
    
    • 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

    在这里插入图片描述

    验证token的中间件
    在所有路由之前定义一个中间件来验证token是否存在,以及token是否有效

    const verifyToken = (req, res, next) => {
        // 获取请信息中的token
        const token = req.query.token;
        // 如果token不存在,则返回错误信息
        if (!token) {
          return res.json({ code:401,message: '未提供token' });
        }
        try {
          // 验证token是否有效
          const decoded = jwt.verify(token, '147258');
          // 将解码后的token信息保存到请求对象中
          req.user = decoded;
          next();
        } catch (err) {
          return res.json({ code:403,message: 'token验证失败' });
        }
      }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    退出接口
    退出接口需要在token验证有效后才可以请求,具体的操作就是删除token即可

    app.post('/user/logout',verifyToken, (req, res) => {
        // 删除token
        res.clearCookie('token');
        res.json({
            code: 0,
            message: '退出成功'
        });
    });
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    获取用户信息接口
    从中间件中拿到解析出来的user对象,并拿到里面的username属性
    根据该属性判断用户的角色,之后返回给前端,前端可以用来做权限判定

    //获取用户信息接口
    app.get('/user/getInfo',verifyToken,(req,res)=>{
        const username =req.user.username
        if(username==='admin'){
            res.json({
                code:200,
                roles:'admin',
                introduction: 'I am a super administrator',
                avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
                name: '超级管理员'
            })
        }else if(username === 'editor'){
            res.json({
                code:200,
                roles:'editor',
                introduction: 'I am an editor',
                avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
                name: '编辑'
            })
        }else{
            res.json({
                code:201,
                roles:'未找到改该角色'
            })
        }
    
    })
    
    
    • 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

    增删改查功能
    这些功能都需要登录后才可以操作,请求都需要携带token

    // 查询
    app.get('/books/getAll',verifyToken,(req, res) => {
        query('SELECT * FROM book', (err, results) => {
            if (err) throw err;
            res.json({code:0,message: '获取数据成功',data:results})
        })
    })
    
    // 新增
    app.post('/books/add',verifyToken,(req, res) => {
        const {title, author, publisher, publish_date, price} = req.body
        query(`INSERT INTO book (title, author, publisher, publish_date, price) VALUES ('${title}', '${author}', '${publisher}', '${publish_date}', '${price}')`, (err, results) => {
            if (err) throw err;
            res.json({code:0,message: '新增成功', id: results.insertId})
        })
    })
    
    // 修改
    app.put('/books/put/:id',verifyToken, (req, res) => {
        const {title, author, publisher, publish_date, price} = req.body
        query(`UPDATE book SET title='${title}', author='${author}', publisher='${publisher}', publish_date='${publish_date}', price='${price}' WHERE id=${req.params.id}`, (err, results) => {
            if (err) throw err;
            res.json({code:0,message: '修改成功', id: req.params.id})
        })
    })
    
    // 删除
    app.delete('/books/delete/:id',verifyToken, (req, res) => {
        query(`DELETE FROM book WHERE id=${req.params.id}`, (err, results) => {
            if (err) throw err;
            res.json({code:0,message: '删除成功', id: req.params.id})
        })
    })
    
    
    • 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

    这里token我直接就使用params方式传参,因为我测试环境的前端就是这样写的,也可以使用用header来传token,看自己的需求
    在这里插入图片描述

    到目前为止,一个完整的小案列就算完成了!

    完整代码
    以下是完整的代码,依赖需要自己安装,具体的版本前面有写过

    const express = require('express')
    const mysql = require('mysql')
    const cors = require('cors')
    const jwt = require('jsonwebtoken')
    const md5 = require('md5')
    const bodyParser = require('body-parser');
    const app = express()
    app.use(bodyParser.json())              //解析json
    app.use(bodyParser.urlencoded({ extended: true }));             //解析客户端传递过来的参数 
    
    app.use((req, res, next) => {               //解决跨域问题,能够允许所有网址和方法的跨域处理
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE, OPTIONS');
        res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
        if ('OPTIONS' === req.method) {
          res.sendStatus(200);
        } else {
          next();
        }
      });
      
    
    
    // let db = mysql.createConnection({
    //     host:'127.0.0.1',
    //     user:'root',
    //     password:'123456',
    //     database:'books',
    //     port:3306
    // })
    
    // db.connect((err)=>{
    //     if(err) throw err;
    //     console.log('连接成功')    
    // })
    
    // setInterval(function(){
    //     db.query('select 1')
    // },5000);
    
    // 创建连接池
    const pool = mysql.createPool({
        host: '127.0.0.1',
        user: 'root',
        password: '123456',
        database: 'books',
        connectionLimit: 10 // 设置连接池最大连接数
      });
      
      // 查询函数
      function query(sql, callback) {
        // 从连接池中获取一个连接
        pool.getConnection((err, connection) => {
          if (err) {
            callback(err, null);
          } else {
            // 执行查询
            connection.query(sql, (err, results) => {
              // 释放连接
              connection.release();
              callback(err, results);
            });
          }
        });
      }
      
      // 定时任务
      setInterval(() => {
        const sql = 'SELECT 1';
        query(sql, (err, results) => {
          if (err) {
            console.error(err);
          } else {
            // console.log('查询结果:', results);
          }
        });
      }, 5000);
    //这段代码是为了保持数据库连接处于活动状态,
    // 它会每隔5秒钟向数据库发送一个select 1的查询请求,确保连接不会因为长时间没有交互而被断开。
    // 这个技巧被称为“保活”,可以让长时间运行的应用程序保持稳定的连接状态。
    
    // 定义一个中间件来验证token,是否需要登录才可以操作
    const verifyToken = (req, res, next) => {
        // 获取请信息中的token
        const token = req.query.token;
        // 如果token不存在,则返回错误信息
        if (!token) {
          return res.json({ code:401,message: '未提供token' });
        }
        try {
          // 验证token是否有效
          const decoded = jwt.verify(token, '147258');
          // 将解码后的token信息保存到请求对象中
          req.user = decoded;
          next();
        } catch (err) {
          return res.json({ code:403,message: 'token验证失败' });
        }
      }
      
    
    // 登录接口
    app.post('/user/login', (req, res) => {
        const { username, password } = req.body;
    
        if(!username || !password) return res.json({ code:403,message: '用户名或密码不能为空' });
        // 进行MD5加密
        const md5Pwd = md5(password);
        // 查询数据库中是否存在该用户
        const sql = `SELECT * FROM user WHERE username='${username}' AND password='${md5Pwd}'`;
        query(sql, (err, result) => {
            if (err) throw err;
            if (result.length === 0) {
                res.json({
                    code: 1,
                    message: '用户名或密码错误'
                });
            } else {
                // 验证成功,生成token并返回
                const payload = {username: username}; // 按照需求设置payload
                const secretKey = '147258'; // 按照需求设置密钥
                const token = jwt.sign(payload, secretKey, {expiresIn: '1h'}); // 生成token,设置过期时间1小时
    
                // 将token返回给客户端
                res.json({
                    code: 200,
                    token:token,
                    message: '登录成功'
                });
            }
        });
    });
    
    //获取用户信息接口
    app.get('/user/getInfo',verifyToken,(req,res)=>{
        const username =req.user.username
        if(username==='admin'){
          res.send({
            code:200,
            data:{
              roles:'admin',
              introduction: 'I am a super administrator',
              avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
              name: '超级管理员'
            }
              
           })
        }else if(username === 'editor'){
            res.json({
                code:200,
                  roles:'editor',
                  introduction: 'I am an editor',
                  avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
                  name: '编辑'
            })
        }else{
            res.json({
                code:201,
                roles:'未找到改该角色'
            })
        }
    
    })
    
    // 退出接口
    app.post('/user/logout',verifyToken, (req, res) => {
        // 清除登录状态
        // 删除token
        res.clearCookie('token');
        res.json({
            code: 200,
            message: '退出成功'
        });
    });
    
    
    // 查询
    app.get('/books/getAll',verifyToken,(req, res) => {
        query('SELECT * FROM book', (err, results) => {
            if (err) throw err;
            res.json({code:0,message: '获取数据成功',data:results})
        })
    })
    
    // 新增
    app.post('/books/add',verifyToken,(req, res) => {
        const {title, author, publisher, publish_date, price} = req.body
        query(`INSERT INTO book (title, author, publisher, publish_date, price) VALUES ('${title}', '${author}', '${publisher}', '${publish_date}', '${price}')`, (err, results) => {
            if (err) throw err;
            res.json({code:0,message: '新增成功', id: results.insertId})
        })
    })
    
    // 修改
    app.put('/books/put/:id',verifyToken, (req, res) => {
        const {title, author, publisher, publish_date, price} = req.body
        query(`UPDATE book SET title='${title}', author='${author}', publisher='${publisher}', publish_date='${publish_date}', price='${price}' WHERE id=${req.params.id}`, (err, results) => {
            if (err) throw err;
            res.json({code:0,message: '修改成功', id: req.params.id})
        })
    })
    
    // 删除
    app.delete('/books/delete/:id',verifyToken, (req, res) => {
        query(`DELETE FROM book WHERE id=${req.params.id}`, (err, results) => {
            if (err) throw err;
            res.json({code:0,message: '删除成功', id: req.params.id})
        })
    })
    
    app.listen(3001,()=>{
        console.log('serve is running at http://127.0.0.1:3001')
    })
    
    • 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
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
  • 相关阅读:
    星火模型(Spark)的langchain 实现
    电脑照片如何打包发送微信?三种方法随心选!
    单线程介绍、ECMAScript介绍、操作系统Windows、Linux 和 macOS
    Sql文件导入数据库-保姆级教程
    什么是大票零担?ZETA如何实现大票零担货物追踪可视化?
    玻璃表面修饰DNA|DNA修饰的上转换纳米材料|DNA-UCNPs实验原理
    Docker Swarm集群部署
    mongdb shell无法链接数据库
    Spring(ioc)
    宋浩概率论笔记(八)假设检验
  • 原文地址:https://blog.csdn.net/formylovetm/article/details/136200424