-
Notifications
You must be signed in to change notification settings - Fork 148
/
app.js
executable file
·94 lines (85 loc) · 4.52 KB
/
app.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var PORT = process.argv[2] || 8000;
var http = require("http");
var url = require("url");
var fs = require("fs");
var path = require("path");
var mime = require("./mime").types;
var config = require("./config");
var utils = require("./utils");
var zlib = require("zlib");
var server = http.createServer(function(request, response) {
response.setHeader("Server", "Node/V5");
response.setHeader('Accept-Ranges', 'bytes');
var pathname = url.parse(request.url).pathname;
if (pathname.slice(-1) === "/") {
pathname = pathname + config.Welcome.file;
}
var realPath = path.join("assets", path.normalize(pathname.replace(/\.\./g, "")));
var pathHandle = function (realPath) {
fs.stat(realPath, function (err, stats) {
if (err) {
response.writeHead(404, "Not Found", {'Content-Type': 'text/plain'});
response.write("This request URL " + pathname + " was not found on this server.");
response.end();
} else {
if (stats.isDirectory()) {
realPath = path.join(realPath, "/", config.Welcome.file);
pathHandle(realPath);
} else {
var ext = path.extname(realPath);
ext = ext ? ext.slice(1) : 'unknown';
var contentType = mime[ext] || "text/plain";
response.setHeader("Content-Type", contentType);
var lastModified = stats.mtime.toUTCString();
var ifModifiedSince = "If-Modified-Since".toLowerCase();
response.setHeader("Last-Modified", lastModified);
if (ext.match(config.Expires.fileMatch)) {
var expires = new Date();
expires.setTime(expires.getTime() + config.Expires.maxAge * 1000);
response.setHeader("Expires", expires.toUTCString());
response.setHeader("Cache-Control", "max-age=" + config.Expires.maxAge);
}
if (request.headers[ifModifiedSince] && lastModified == request.headers[ifModifiedSince]) {
response.writeHead(304, "Not Modified");
response.end();
} else {
var compressHandle = function (raw, statusCode, reasonPhrase,contentLength) {
var stream = raw;
var acceptEncoding = request.headers['accept-encoding'] || "";
var matched = ext.match(config.Compress.match);
if (matched && acceptEncoding.match(/\bgzip\b/)) {
response.setHeader("Content-Encoding", "gzip");
stream = raw.pipe(zlib.createGzip());
} else if (matched && acceptEncoding.match(/\bdeflate\b/)) {
response.setHeader("Content-Encoding", "deflate");
stream = raw.pipe(zlib.createDeflate());
}
else{
response.setHeader('Content-Length',contentLength);
}
response.writeHead(statusCode, reasonPhrase);
stream.pipe(response);
};
if (request.headers["range"]) {
var range = utils.parseRange(request.headers["range"], stats.size);
if (range) {
response.setHeader("Content-Range", "bytes " + range.start + "-" + range.end + "/" + stats.size);
var raw = fs.createReadStream(realPath, {"start": range.start, "end": range.end});
compressHandle(raw, 206, "Partial Content",(range.end - range.start + 1));
} else {
response.writeHead(416, "Request Range Not Satisfiable");
response.end();
}
} else {
var raw = fs.createReadStream(realPath);
compressHandle(raw, 200, "Ok",stats.size);
}
}
}
}
});
};
pathHandle(realPath);
});
server.listen(PORT);
console.log("Server running at port: " + PORT + ".");