-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (53 loc) · 1.82 KB
/
index.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
var ServerConttroller = function (params) {
this.sys = require("util");
this.my_http = require("http");
this.path = require("path");
this.url = require("url");
this.filesys = require("fs");
};
ServerConttroller.prototype.createServer = function () {
var self = this;
return this.my_http.createServer(function (request, response) {
console.log("request: ", request.url);
switch (request.url) {
case '/':
self.index(response, '/index.html');
break;
case '/sayHallo':
self.sayHallo(response);
default:
self.index(response, request.url);
}
}).listen(8080);
};
ServerConttroller.prototype.index = function (response, my_url) {
var self = this;
var full_path = this.path.join(process.cwd(), my_url);
this.filesys.exists(full_path, function (path_exists) {
if (!path_exists) {
response.writeHeader(404, { "Content-Type": "text/plain" });
response.write("404 Not Found\n");
response.end();
}
else {
self.filesys.readFile(full_path, "binary", function (err, file) {
if (err) {
response.writeHeader(500, { "Content-Type": "text/plain" });
response.write(err + "\n");
response.end();
}
else {
response.writeHeader(200);
response.write(file, "binary");
response.end();
}
});
}
});
};
ServerConttroller.prototype.sayHallo = function (response) {
response.writeHeader(200, { "Content-Type": "text/plain" });
response.write('Hallo', 'binary');
response.end();
};
module.exports = ServerConttroller;