美文网首页
node连接mysql/安装mysql模块/使用crypto加密

node连接mysql/安装mysql模块/使用crypto加密

作者: 子心_ | 来源:发表于2019-07-09 09:47 被阅读0次

    node连接mysql

        运行cnpm i mysql -D,安装mysql模块;
        创建mysql对象:
        const mysql = require('mysql');
        创建连接池:
        let db = mysql.createPool({host:'localhost',user:'root',password:'root',port:3306});
        接下来操作get或者post方法进行数据库交互即可,
        使用db.query('sql',(err,data)=>{});
    

    使用crypto 进行加密

        const crypto = require('crypto');
        function md5(str){//此处使用函数
            let obj = crypto.createHash('md5');//使用md5加密模式
            obj.update(str);//开始加密
            return obj.digest('hex');//加密后的数据,hex为16进制;
        }
        使用md5(pass) 即可进行加密;
    

    WebSocket

    socket.io 一个WebSocket的库
    安装socket.io
    cnpm i socket.io -D
    使用:
    const io = require('socket.io');//使用socket模块;
    const http = require('http');
    let httpServer = http.createServer();//创建一个http服务;
    httpServer.listen(8087);//监听端口;
    let wsServer = io.listen(httpServer);//socket监听http服务,并返回一个WebSocket服务;
    wsServer.on('connection',sock=>{
        //当有人连接时,走sock,sock.emit//用来发送,sock.on//用来接收
    })
    
        HTML文件中需要引用
        <script src="http://localhost:8087/socket.io/socket.io.js" charset="utf-8"></script>
        <script>
            let sock = io.connect('ws://localhost:8087/');//连接服务器
            sock.emit();//用来发送,两个参数 事件 与数据
            sock.on();//用来接收, 两个参数  事件 与回调函数,回调函数的参数是数据
        </script>
    

    扩展

    相关文章

      网友评论

          本文标题:node连接mysql/安装mysql模块/使用crypto加密

          本文链接:https://www.haomeiwen.com/subject/nwytkctx.html