forked from Fred-el-Jolo/crazy-wars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
39 lines (33 loc) · 863 Bytes
/
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
/**
* Define Router class
*/
define(['url'], function(url) {
// attributes
var Router = function() {
// routes
this.routes = {};
};
//methods
Router.prototype = {
add: function(sUri, fn) {
this.routes[sUri] = fn;
},
resolve: function(oRequest, oResponse) {
var oUrl = url.parse(oRequest.url);
var oRoute = oUrl.pathname + '.' + oRequest.method.toLowerCase();
var fn = this.routes[oRoute];
if (!fn){
if (oRoute.indexOf('/lib/') >= 0 || oRoute.indexOf('/js/') >=0 ){
fn = this.routes['/static-ressource.get'];
}
}
if(typeof fn === "function"){
fn.call(this, oRequest, oResponse);
console.log("router : access on route " + oRoute);
}
/*else
console.log("router : no route for this pathname");*/
}
};
return new Router();
});