forked from nwjs/nw.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
87 lines (73 loc) · 2.32 KB
/
server.js
File metadata and controls
87 lines (73 loc) · 2.32 KB
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
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
config = require('./config');
ports = new Array(8123,8124),
servers = new Array(ports.length);
var res_save = new Array();
types = {
".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"
};
function request_listener(req, res) {
var pathname=__dirname+url.parse(req.url).pathname;
console.log("Receive request");
if (path.extname(pathname)=="") {
pathname+="/";
}
if (pathname.charAt(pathname.length-1)=="/"){
pathname+="index.html";
}
path.exists(pathname,function(exists){
if(exists){
var ext = path.extname(pathname);
var content_type = types[ext] || "text/plain";
res.setHeader("Content-Type", content_type);
fs.stat(pathname, function (err, stat) {
var last_modified = stat.mtime.toUTCString();
var if_modified_since = "If-Modified-Since".toLowerCase();
res.setHeader("Last-Modified", last_modified);
if (req.headers[if_modified_since] && last_modified == req.headers[if_modified_since]) {
res_save.push({"status": 304, 'pathname':req.url.slice(1)});
res.writeHead(304, "Not Modified");
res.end();
} else {
res_save.push({"status": 200, 'pathname':req.url.slice(1)});
fs.readFile(pathname,function (err,data){
res.end(data);
});
}
});
} else {
res.writeHead(404, {"Content-Type": "text/html"});
res.end("<h1>404 Not Found</h1>");
}
});
}
function error_handler(e) {
console.log("error code: ", e.code);
}
for (var i = 0; i < ports.length; i++) {
servers[i] = http.createServer(request_listener);
servers[i].listen(ports[i], "127.0.0.1");
servers[i].on('error', error_handler);
console.log("Server running at http://127.0.0.1:" + ports[i]);
}
exports.res_save = res_save;