-
Notifications
You must be signed in to change notification settings - Fork 18
/
Routes.js
137 lines (124 loc) · 3.74 KB
/
Routes.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var Routes = (function() {
var _ = {}, ctor = function(){};
_.bind = function bind(func, context) {
var bound, args, slice = Array.prototype.slice;
args = slice.call(arguments, 2);
return bound = function() {
if (!(this instanceof bound)) return func.apply(context, args.concat(slice.call(arguments)));
ctor.prototype = func.prototype;
var self = new ctor;
var result = func.apply(self, args.concat(slice.call(arguments)));
if (Object(result) === result) return result;
return self;
};
};
var Res = function Res(res) {
this.headers = res.headers || {};
this.res = res;
this.statusCode = res.statusCode;
};
Res.prototype.header = function(h, v) {
if (v) return this.headers[h] = v;
else return this.headers[h];
};
Res.prototype.send = function(body) {
if (arguments.length == 2) {
if (typeof body != 'number' && typeof arguments[1] == 'number') {
this.statusCode = arguments[1];
} else {
this.statusCode = body;
body = arguments[1];
}
}
if (typeof body == 'number') { this.statusCode = body; body = ''; }
else if (typeof body == 'object') body = JSON.stringify(body);
this.close(body);
return this;
};
Res.prototype.write = function(data) {
this.res.write(data);
return this;
};
Res.prototype.redirect = function(url) {
this.header('Location', url);
this.send(301);
};
Res.prototype.close = function(data) {
this.res.statusCode = this.statusCode;
this.res.headers = this.headers || {};
this.write(data);
this.res.close();
return this;
};
var R = function Routes() {
this.server = require('webserver').create();
this.routes = [];
};
R.prototype.preRoute = function(req, res) {
this.router.call(this, req, new Res(res));
};
R.prototype.router = function(req, res, i) {
var i = i || 0;
for (i; i < this.routes.length; i++) {
var route = this.routes[i];
if (route.method == 'ALL' || route.method == req.method) {
var match = req.url.match(route.route);
if (match) {
req.params = match.slice(1);
try {
return route.handler.call(this, req, res, _.bind(this.router, this, req, res, ++i));
} catch (err) {
console.log(err.stack);
return res.send(err.stack, 500);
}
}
}
}
res.send('Not found', 404);
};
R.prototype.addRoute = function(method, route, handler) {
if (!(route instanceof RegExp)) route = new RegExp("^" + route + "$");
this.routes.push({method: method, route: route, handler: handler});
};
R.prototype.all = function(route, handler) {
this.addRoute('ALL', route, handler);
};
R.prototype.get = function(route, handler) {
this.addRoute('GET', route, handler);
};
R.prototype.post = function(route, handler) {
this.addRoute('POST', route, handler);
};
R.prototype.head = function(route, handler) {
this.addRoute('HEAD', route, handler);
};
R.prototype.put = function(route, handler) {
this.addRoute('PUT', route, handler);
};
R.prototype.delete = function(route, handler) {
this.addRoute('DELETE', route, handler);
};
R.prototype.use = function(handler) {
this.addRoute('ALL', /.+/, handler);
};
R.prototype.listen = function(port) {
this.server.listen(port, _.bind(this.preRoute, this));
};
R.static = function(root) {
var fs = require('fs'),
root = fs.absolute(root);
return function(req, res, next) {
if (req.method != 'GET') return next();
var resource = req.url.slice(1),
path = root + '/' + resource;
if (resource && fs.isFile(path) && fs.isReadable(path)) {
var file = fs.read(path);
res.send(file);
} else {
next();
}
}
};
return R;
})();
if (module && module.exports) module.exports = Routes;