# 热更新

# electron-asar-hot-updater

npm install electron-asar-hot-updater -s

electron-vue Demo (opens new window)

# 服务端代码示例(node.js实现文件下载)

const hostname = '0.0.0.0';
const port = 3176;
const http = require('http');
const server = http.createServer();
const fs = require('fs');
const path = require('path');

server.listen(port, hostname, () => {
    let msg = `开启服务 http://${hostname}:${port}/`;
    console.log("🚀 ~ msg", msg)
});

server.on('error', function(err) {
    console.log("🚀 ~ err", err)
    if (err.code === 'EADDRINUSE') {
        console.log('端口【' + port + '】 已经被使用')
    } else {
       
    }
});

server.on('request', async function(req, res) {
    const requestUrl = req.url;
    
    let data = [];
    req.on('data', chunk => {
        //req对象启动data方法,此方法将会多次获取提交的数据,如果提交的数据量大的话。
        data += chunk;
    });

    req.on('end', async () => {
        if(requestUrl === '/api/apitest/postElectronUpdate') {
            res.setHeader('Content-Type', 'text/plain;charset=UTF-8');
            console.log('ok')
            res.end(JSON.stringify( {
                "name": "app",
                "version": "1.1.3",
                "asar": "http://127.0.0.1:3176/update.zip",
                "info": "1.fix bug\n2.feat..."
            }));
        } else {
            var zip = fs.createReadStream(path.join(__dirname,'./update.zip'));
            res.writeHead(200, {
                'Content-Type': 'application/zip',
                'Content-Disposition': 'attachment; filename=update.zip'
            });
            zip.pipe(res);
        }
    });
});
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