-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (53 loc) · 1.62 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
62
63
64
65
66
var util = require('util'),
EventEmitter = require('events').EventEmitter,
Route = require('./route');
function Rudder() {
this.routes = [];
}
util.inherits(Rudder, EventEmitter);
Rudder.prototype.parseUrl = function(req) {
return req.url.split("?")[0];
}
Rudder.prototype.loopOver = function(req, res, path) {
for(var index in this.routes) {
var route = this.routes[index];
if (route.didMatch(req.method, path)) {
route.execute(req, res, path, this);
return true;
}
}
return false;
}
Rudder.prototype.middleware = function() {
var self = this;
return function(req, res, next) {
if (!self.loopOver(req, res, self.parseUrl(req))) {
next();
}
return;
}
}
Rudder.prototype.addRoute = function(method, regex, middleware, func) {
var route = new Route(method, regex, middleware, func);
this.routes.unshift(route);
return route;
}
Rudder.prototype.get = function(regex, middleware, func) {
return this.addRoute("GET", regex, middleware, func);
}
Rudder.prototype.post = function(regex, middleware, func) {
return this.addRoute("POST", regex, middleware, func);
}
Rudder.prototype.put = function(regex, middleware, func) {
return this.addRoute("PUT", regex, middleware, func);
}
Rudder.prototype.del = function(regex, middleware, func) {
return this.addRoute("DELETE", regex, middleware, func);
}
Rudder.prototype.head = function(regex, middleware, func) {
return this.addRoute("HEAD", regex, middleware, func);
}
Rudder.prototype.any = function(regex, middleware, func) {
return this.addRoute(null, regex, middleware, func);
}
module.exports = Rudder;