-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhttp-etag.js
More file actions
52 lines (44 loc) · 1.31 KB
/
Copy pathhttp-etag.js
File metadata and controls
52 lines (44 loc) · 1.31 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
const http = require('http');
const url = require('url');
const fs = require('fs');
const checksum = require('checksum');
const mime = require('mime');
function getMimeType(res) {
const EXT_MIME_TYPES = mime.types;
const path = require('path');
const mime_type = EXT_MIME_TYPES[path.extname(res).slice(1) || 'html'];
return mime_type;
}
const server = http.createServer((req, res) => {
const srvUrl = url.parse(`http://${req.url}`);
let path = srvUrl.path;
if(path === '/') path = '/index.html';
const resPath = `resource${path}`;
if(!fs.existsSync(resPath)) {
res.writeHead(404, {'Content-Type': 'text/html'});
return res.end('<h1>404 Not Found</h1>');
}
checksum.file(resPath, (err, sum) => {
const resStream = fs.createReadStream(resPath);
sum = `"${sum}"`; // etag 要加双引号
if(req.headers['if-none-match'] === sum) {
res.writeHead(304, {
'Content-Type': getMimeType(resPath),
etag: sum,
});
res.end();
} else {
res.writeHead(200, {
'Content-Type': getMimeType(resPath),
etag: sum,
});
resStream.pipe(res);
}
});
});
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());
});