forked from saturngod/piControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
piRoute.js
executable file
·106 lines (90 loc) · 1.58 KB
/
piRoute.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
var url = require('url');
var fs = require("fs");
var req , res;
function routing(address,callback)
{
var url_parts = url.parse(req.url);
var current_url=url_parts.pathname;
if(current_url == address)
{
path=current_url.match(address);
callback(req,res,path);
}
}
function notfound()
{
res.writeHead(404);
res.end("not found");
}
function get(address,callback)
{
if(req.method == "GET")
{
return routing(address,callback);
}
else {
notfound();
}
}
function put(address,callback)
{
if(req.method == "PUT")
{
return routing(address,callback);
}
else {
notfound();
}
}
function del(address,callback)
{
if(req.method == "DELETE")
{
return routing(address,callback);
}
else {
notfound();
}
}
function post(address,callback)
{
if(req.method == "POST")
{
return routing(address,callback);
}
else {
notfound();
}
}
function init(_req,_res)
{
req = _req;
res = _res;
}
function staticFiles(extensions)
{
//Regular expression is
//[A-Za-z0-9@]+.(js|png|css)
var exts = extensions.split("|");
var reg = "[A-Za-z0-9@]+.(";
for (var i = 0; i < exts.length; i++) {
reg = reg + exts[i] + "|";
};
reg = reg.substring(0, reg.length - 1);
reg = reg + ")";
var staticRegular = new RegExp(reg);
var url_parts = url.parse(req.url);
var current_url=url_parts.pathname;
if(staticRegular.test(current_url))
{
var fileData = fs.readFileSync(__dirname+current_url);
res.end(fileData);
}
}
exports.init = init;
exports.get = get;
exports.post = post;
exports.put = put;
exports.notfound = notfound;
exports.del = del;
exports.staticFiles = staticFiles;