-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
56 lines (49 loc) · 1.3 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
let http = require('http');
let fs = require('fs');
let path = require('path');
let root = '.';
const PORT = 9999;
const DEFAULT_URL = '/index.html';
const getContentType = (suffix) => {
let suffixMap = {
js: 'text/javascript',
css: 'text/css',
html: 'text/html',
ico: 'image/x-icon'
};
return suffixMap[suffix] ?
{
'content-type': suffixMap[suffix]
}
:
{};
};
http.createServer((req, res) => {
let url = req.url.substr(1) || DEFAULT_URL;
let queryIndex = url.indexOf('?');
let query = '';
if (queryIndex !== -1) {
url = url.substring(0, queryIndex);
}
let filePath = path.resolve(__dirname, url);
let httpCode = null;
let content = null;
if (!url.includes('.')) {
filePath += '.js'
}
console.log(url, filePath);
fs.readFile(filePath, function (err, data) {
if (err) {
httpCode = 404;
content = '404';
} else {
httpCode = 200;
content = data;
}
let suffixMatch = url.match(/\.(\w+)/);
res.writeHeader(httpCode, getContentType(suffixMatch ? suffixMatch[1] : 'js'));
res.write(content);
res.end();
});
}).listen(PORT);
console.log('服务器开启成功', PORT);