-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhttp-compression.js
More file actions
84 lines (79 loc) · 2.61 KB
/
http-compression.js
File metadata and controls
84 lines (79 loc) · 2.61 KB
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
const http = require('http');
const url = require('url');
const path = require('path');
const fs = require('fs');
const mime = require('mime');
const zlib = require('zlib');
const server = http.createServer((req, res) => {
let filePath = path.resolve(__dirname, path.join('www', url.fileURLToPath(`file:///${req.url}`)));
console.log(`Request: ${filePath}`);
if(fs.existsSync(filePath)) {
const stats = fs.statSync(filePath);
if(stats.isDirectory()) {
filePath = path.join(filePath, 'index.html');
}
if(fs.existsSync(filePath)) {
const {ext} = path.parse(filePath);
const stats = fs.statSync(filePath);
const timeStamp = req.headers['if-modified-since'];
let status = 200;
if(timeStamp && Number(timeStamp) === stats.mtimeMs) {
status = 304;
}
const mimeType = mime.getType(ext);
const responseHeaders = {
'Content-Type': mimeType,
'Cache-Control': 'max-age=86400', // 缓存一天
'Last-Modified': stats.mtimeMs,
};
const acceptEncoding = req.headers['accept-encoding'];
const compress = acceptEncoding && /^(text|application)\//.test(mimeType);
if(compress) {
acceptEncoding.split(/\s*,\s*/).some((encoding) => {
if(encoding === 'gzip') {
responseHeaders['Content-Encoding'] = 'gzip';
return true;
}
if(encoding === 'deflate') {
responseHeaders['Content-Encoding'] = 'deflate';
return true;
}
if(encoding === 'br') {
responseHeaders['Content-Encoding'] = 'br';
return true;
}
return false;
});
}
const compressionEncoding = responseHeaders['Content-Encoding'];
res.writeHead(status, responseHeaders);
if(status === 200) {
const fileStream = fs.createReadStream(filePath);
if(compress && compressionEncoding) {
let comp;
if(compressionEncoding === 'gzip') {
comp = zlib.createGzip();
} else if(compressionEncoding === 'deflate') {
comp = zlib.createDeflate();
} else {
comp = zlib.createBrotliCompress();
}
fileStream.pipe(comp).pipe(res);
} else {
fileStream.pipe(res);
}
} else {
res.end();
}
}
} else {
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('<h1>Not Found</h1>');
}
});
server.on('clientError', (err, socket) => {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
server.listen(10080, () => {
console.log('opened server on', server.address());
});