This repository has been archived by the owner on Jun 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
62 lines (53 loc) · 2.21 KB
/
server.js
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
let http = require('http'),
fs = require('fs'),
path = require('path'),
url = require('url'),
zlib = require('zlib');
http.createServer((req, res) => {
let {pathname} = url.parse(req.url),
acceptEncoding = req.headers['accept-encoding'] || '',
referer = req.headers['Referer'] || '',
raw;
console.log('Request: ', req.url);
try {
raw = fs.createReadStream(path.resolve(__dirname, pathname.replace(/^\//, '')));
raw.on('error', (err) => {
console.log(err);
if (err.code === 'ENOENT') {
res.writeHeader(404, {'content-type': 'text/html;charset="utf-8"'});
res.write('<h1>404错误</h1><p>你要找的页面不存在</p>');
res.end();
}
});
if (acceptEncoding.match(/\bgzip\b/)) {
res.writeHead(200, { 'Content-Encoding': 'gzip' });
raw.pipe(zlib.createGzip()).pipe(res);
} else if (acceptEncoding.match(/\bdeflate\b/)) {
res.writeHead(200, { 'Content-Encoding': 'deflate' });
raw.pipe(zlib.createDeflate()).pipe(res);
} else {
res.writeHead(200, {});
raw.pipe(res);
}
} catch (e) {
console.log(e);
}
// fs.readFile(path.resolve(__dirname, pathname.replace(/^\//, '')), (err, data) => {
// if (err) {
// res.writeHeader(404, {'content-type': 'text/html;charset="utf-8"'});
// res.write('<h1>404错误</h1><p>你要找的页面不存在</p>');
// res.end();
// } else {
// res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8088');
// res.writeHead(200, {
// // 'content-type': 'text/html;charset="utf-8"',
// // 'Access-Control-Allow-Origin': '*',
// // 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
// // 'Access-Control-Allow-Headers': 'X-Requested-With, Content-Type'
// });
// res.write(data);
// res.end();
// }
// });
}).listen(8088);
console.log('服务器开启成功', 'localhost:8088/');