-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.js
42 lines (38 loc) · 962 Bytes
/
handlers.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
var fs = require('fs');
var util = require('util');
function root(res, path, query) {
console.log("request handler '/'");
if (path == '/') {
path = "/index.html";
}
fs.readFile(__dirname + path, function(err, data) {
if (err) {
res.writeHead(404);
res.write("lol u fail");
res.end();
} else {
res.writeHead(200, {'Content-Type' : contentType(path)});
res.write(data, 'utf-8');
res.end();
}
});
}
function ip(res, path, query) {
console.log("request handler 'ip'");
res.writeHead(200, {'Content-Type' : 'text/plain'});
res.write(query);
res.end();
}
function contentType(path) {
if (path.match('.js$')) {
return "text/javascript";
}
if (path.match('.css$')) {
return "text/css";
}
if (path.match('.html$')) {
return "text/html";
}
}
exports.root = root;
exports.ip = ip;