-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.js
125 lines (120 loc) · 3.18 KB
/
router.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var dataFile = require("./dataFile.js");
var fs = require("fs");
var defaultCorsHeaders = {
"access-control-allow-origin": "*",
"access-control-allow-methods": "GET, POST, PUT, DELETE, OPTIONS",
"access-control-allow-headers": "content-type, accept",
"access-control-max-age": 10 // Seconds.
};
var headers = defaultCorsHeaders;
var routes = {
"GET" : [
{
pattern: /^\/classes\/([a-z0-9]+)$/i,
method: function(request, response, target){
headers['Content-Type'] = "application/json";
response.writeHead(200, headers);
response.end(JSON.stringify(dataFile.getData(target)));
}
},
{
pattern: /\/$/,
method: function(request,response){
fs.readFile('chatClient/index.html', 'utf8', function(err,data){
headers['Content-Type'] = "text/html";
response.writeHead(200,headers);
response.end(data);
});
}
},
{
pattern:/([^+]*)\.([^+]*)/,
method: function(request,response, path, filetype){
fs.readFile('chatClient/' + path + '.' + filetype, 'utf8', function(err,data){
headers['Content-Type'] = "text/" + filetype;
response.writeHead(200,headers);
response.end(data);
});
}
},
{
pattern: /[^+]*/,
method: function(request, response, target){
headers['Content-Type'] = "application/json";
response.writeHead(404, headers);
response.end(JSON.stringify('\n'));
}
}
],
"POST" : [
{
pattern: /^\/classes\/([a-z0-9]+)$/i,
method: function(request, response, target){
headers['Content-Type'] = "application/json";
request.setEncoding('utf8');
var body = "";
request.on('data', function(data){
body += data;
});
request.on('end', function(data){
try{
dataFile.setData(target,JSON.parse(body));
response.writeHead(201, headers);
} catch (e) {
response.writeHead(400, headers);
console.log(e);
} finally {
response.end(JSON.stringify('\n'));
}
});
}
},
{
pattern: /[^+]*/,
method: function(request, response){
headers['Content-Type'] = "application/json";
response.writeHead(404, headers);
response.end(JSON.stringify('\n'));
}
}
],
"OPTIONS" : [
{
pattern:/[^+]*/,
method: function(request, response){
headers['Content-Type'] = "application/json";
response.writeHead(200, headers);
response.end(JSON.stringify('\n'));
}
}
],
"DELETE": [
{
pattern:/[^+]*/,
method: function(request, response){
headers['Content-Type'] = "application/json";
response.writeHead(501, headers);
response.end(JSON.stringify('\n'));
}
}
],
"PUT": [
{
pattern:/[^+]*/,
method: function(request, response){
headers['Content-Type'] = "application/json";
response.writeHead(501, headers);
response.end(JSON.stringify('\n'));
}
}
]
};
exports.trailblazer = function(pathname,method,request,response){
routes[method].some(function(item){
var matches = pathname.match(item.pattern);
if (matches){
item.method.apply(null, [request, response].concat(matches.slice(1)));
return true;
}
});
};