-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.js
70 lines (69 loc) · 2.38 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
63
64
65
66
67
68
69
70
var http = require("http");
var url = require("url");
var fs = require("fs");
var path = require("path");
var mime = {
"css": "text/css",
"gif": "image/gif",
"html": "text/html",
"ico": "image/x-icon",
"jpeg": "image/jpeg",
"jpg": "image/jpeg",
"js": "text/javascript",
"json": "application/json",
"pdf": "application/pdf",
"png": "image/png",
"svg": "image/svg+xml",
"swf": "application/x-shockwave-flash",
"tiff": "image/tiff",
"txt": "text/plain",
"wav": "audio/x-wav",
"wma": "audio/x-ms-wma",
"wmv": "video/x-ms-wmv",
"xml": "text/xml"
};
var server = http.createServer(function(request, response) {
var pathname = url.parse(request.url).pathname;
var realPath;
if (pathname == '/editor.html') {
realPath = pathname;
} else {
realPath = path.join("public", pathname);
}
if (realPath[0] === '/') {
realPath = realPath.substr(1);
}
console.log(realPath);
fs.exists(realPath, function(exists) {
if (!exists) {
response.writeHead(404, "Not Found", {
'Content-Type': 'text/plain'
});
response.write("This request URL " + pathname + " was not found on this server.");
response.end();
} else {
var ext = path.extname(realPath);
ext = ext ? ext.slice(1) : 'unknown';
var contentType = mime[ext] || "text/plain";
response.setHeader("Content-Type", contentType);
fs.stat(realPath,
function(err, stat) {
var lastModified = stat.mtime.toUTCString();
var ifModifiedSince = "If-Modified-Since".toLowerCase();
response.setHeader("Last-Modified", lastModified);
if (request.headers[ifModifiedSince] && lastModified == request.headers[ifModifiedSince]) {
response.writeHead(304, "Not Modified");
response.end();
} else {
var raw = fs.createReadStream(realPath);
var acceptEncoding = request.headers['accept-encoding'] || "";
response.writeHead(200, "Ok");
raw.pipe(response);
}
});
}
});
});
var PORT = '3000';
server.listen(PORT);
console.log("Server runing at port: " + PORT + '. Visit: http://localhost:3000/editor.html');