-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileServer.js
67 lines (62 loc) · 2.04 KB
/
fileServer.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
63
64
65
66
67
var fs = require('fs'),
url = require('url'),
path = require('path'),
http = require('http');
var root = path.resolve(process.argv[2] || '.');
var server = http.createServer(function (request, response) {
var pathname = url.parse(request.url).pathname;
var filepath = path.join(root, pathname);
fs.stat(filepath, function (err, stats) {
if (!err) {
if(stats.isFile()){
response.writeHead(200, {
"Content-Type": getFileType(pathname),
"Cache-Control": (path.extname(pathname) !== ".js") ? "max-age=315360000" : "no-store"
});
fs.createReadStream(filepath).pipe(response);
}else if(stats.isDirectory()){
var defaults = 'index.html';
if (fs.existsSync(path.join(filepath,defaults))) {
filepath = path.join(filepath,defaults);
response.writeHead(200);
fs.createReadStream(filepath).pipe(response);
return;
}
console.log('404' + request.url);
response.writeHead(404);
response.end('404 Not Found');
}
} else {
console.log('404 ' + request.url);
response.writeHead(404);
response.end('404 Not Found');
}
});
});
//获取文件类型
function getFileType(pathname){
switch(path.extname(pathname)){
case ".html":
return "text/html";
break;
case ".js":
return "text/javascript";
break;
case ".css":
return "text/css";
break;
case ".gif":
return "image/gif";
break;
case ".jpg":
return "image/jpeg";
break;
case ".png":
return "image/png";
break;
default:
return "application/octet-stream";
}
}
server.listen(3005);
console.log('Server is running at http://localhost:3005/');