关于7.1 auto.js后台 新的知识点

http长连接 webstocket node express_ws

const server=ws.createServer(connect=>{
    console.log('用户已连接');
    connect.on('text',data=>{
        console.log("用户传来了信息");
        connect.send(data)
    })

    connect.on('close',()=>{
        console.log("连接中断");
    })

    connect.on("error",()=>{
        console.log("用户连接异常 ");
    })
})
server.listen(9090,()=>{
    console.log("服务启动成功,端口号9090======>");
})

element-ui 使用upload上传文件并解决跨域问题

 <el-upload
class="upload-demo"
drag
multiple
:auto-upload="false"
:http-request="upload" //
action=""
ref="upload"

>
//自定义
// 上传文件事件
upload(){
const formData = new FormData();
const file = this.$refs.upload.uploadFiles[0];
const headerConfig = {Headers:{'Content-Type':'multipart/form-data'}}
if(!file){
//如果没有选择文件
this.$message.error('请选择文件');
return
}
//将文件信息添加
formData.append('file',file.raw);
//发送请求到后端
axios.post('/fileUpload',formData,headerConfig).then(res=>{
console.log(res);
})
}

node express 文件上传功能

const mutipart = require('connect-multiparty');
let mutipartMiddware = mutipart();

app.use(mutipart({ uploadDir: './uploads'}));

app.post('/fileUpload', mutipartMiddware ,function(req, res){

// 上传的文件在req.files中
console.log(req.files)
let files = req.files.null
let path = req.files.null.path
let size = req.files.null.size


})

功能板块 7.1 日

node express 文件下载功能 AJAX

前台代码
<form action="http://auto.anrookie.cn:9090/download"
method="post"
enctype="application/x-www-form-urlencoded"
>
<el-button type="primary" native-type="submit" round>点击下载云控客户端</el-button>
</form>

后台代码


// 下载客户端软件的接口
app.post('/download',function(req,res){
res.header("Access-Control-Allow-Origin", "*");    //允许的header类型
res.header("Access-Control-Allow-Headers", "content-type");    //跨域允许的请求方式 
res.header("Access-Control-Allow-Methods", "DELETE,PUT,POST,GET,OPTIONS");
// express 4.x res.download 方法 不支持axios请求
res.download("./apk/base.apk",err=>{
if(err){
res.send("下载失败!");
}else{
console.log("下载成功!");
}
});
});

前台登录的请求接口

//实例代码
app.post('/login', function (req, res) {
let flag = false;
console.log(req.query)
let sql = `select * from userinfo`
connection.query(sql, function (err, result) {
if (err) {
res.send({ code: 001, msg: "连接失败", })
} else {
for (let i in result) {
if (result[i].username == req.query.username && result[i].password == req.query.password) {
flag = true;
break;
}
}
if (flag) {
res.send({
code: 200,
msg: '登录成功',
Token: 'Bera1242432567tgdhbshdgyu34y23885723',
})
} else {
res.send({ code: 001, msg: "登录失败", })
}
}
})
})

关于客户端定时的webSocket请求至服务端 测重问题

const  server = ws.createServer( connect=>{

connectArray[token] = connect;
//当接收到信息时对传来的数据 对比数据库根据ID做判断 如果查到有该条数据则为存在 如果没有则填入该条数据
connect.on('text',val=>{
let data = JSON.parse(val);
phoneid = data.id;
//根据ID查找
let sql1 = `select * from phone where id = '${data.id}' `
connection.query(sql1,(err,res)=>{
if(err){
console.log(err)
}else{
if(res.length!=0){
console.log('已经存在咯')
})
}else{
let sql = `insert into phone (id,name,ip,time,status,status2) values('${data.id}','${data.name}','${data.ip}','${data.time}','${data.status}','0' )`
connection.query(sql,function(err,res){
if(err){
console.log(err);
}
else{
console.log('上线成功啦~')
}
})
}
}
})