(数据库续)
数据库操作实例(注册和登录功能)
- 数据库结构
-
接口
注册:
/reg?user=xxx&pass=xxx
=>{err:0, msg:'原因'}
登录:
/reg?user=xxx&pass=xxx
=>{err:0, msg:'原因'}
文件压缩
let rs = fs.createReadStream(`www${pathname}`);
let gz = zlib.createGzip();
//这个是错的,不要writeHeader,writeHeader会把所有的Header重写了,会报错
// res.writeHeader('Content-Encoding', 'gzip');
//所以要用setHeader
res.setHeader('Content-Encoding', 'gzip');
rs.pipe(gz).pipe(res);
rs.on(`error`, err=>{
res.writeHeader(404);
res.write('Not Found');
res.end();
})
双层md5加密
let md5Key='fjasdklfjasldfjskldfjsdklfjioewurwe34343324fsdfsdfsdfasf./,.;k,;;fds34bm,l$#43';
function md5(data){
let hash = crypto.createHash('md4');
hash.update(data);
return hash.digest('hex');
}
//双层md5加密
function md5_2(data){
return md5(md5(data) + md5Key);
}
WebSocket
- 双向通信
- 自动跨域
- 性能高
例子
//server
const http=require('http');
const io=require('socket.io');
let httpServer = http.createServer();
httpServer.listen(8080);
let wsServer = io.listen(httpServer);
wsServer.on('connection', sock=>{
sock.on('aaa', function(a, b, c){
sock.emit('event', '服务器接收到了消息,返回');
console.log(a, b, c);
})
})
client
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<!-- socket.io/socket.io.js 库文件提供io字段 -->
<script src="http://localhost:8080/socket.io/socket.io.js" charset="utf-8"></script>
<script>
//协议变成ws
let sock = io.connect('ws://localhost:8080/');
sock.emit('aaa', 12, 5, 8);
sock.on('event', function(msg){
alert(msg);
})
</script>
</head>
<body>
</body>
</html>