-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuexpress.js
76 lines (67 loc) · 1.74 KB
/
uexpress.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
module.exports = uExpress;
function uExpress() {
/* router really */
function makeRouter() {
return {
pre_mappings : new Map(),
pre_req_maps : [],
mappings : new Map(),
req_maps : []
};
}
const defaultMap = makeRouter( );
const pre_mappings = defaultMap.pre_mappings;
const pre_req_maps = defaultMap.pre_req_maps;
const mappings = defaultMap.mappings;
const req_maps = defaultMap.req_maps;
return {
all(a,b ) {
if( "string" === typeof a )
pre_mappings.set( a, b ); // b(req,res,next /*next()*/ )
else
pre_req_maps.push( { expr:a, cb:b } );
},
get( a, b ) {
if( "string" === typeof a )
mappings.set( a, b );
else
req_maps.push( { expr:a, cb:b } );
},
post( a, b ) {
if( "string" === typeof a )
mappings.set( a, b );
else
req_maps.push( { expr:a, cb:b } );
},
handle( req, res) {
const parts = req.url.split("?");
const url = unescape(parts[0]);
const filepath = url;//path.dirname(url)+((path.dirname(url)&&path.basename(url))?"/":"")+path.basename(url);
let cb;
let ranOne = false;
let handled = false;
if( cb = pre_mappings.get( filepath ) ) {
let runNext = false;
ranOne = true;
handled = cb( req, res, ()=>{ runNext = true; } );
if( !runNext )
return handled;
}
for( let map of req_maps ) {
if( map.expr.test( filepath ) ) {
//console.log( "expr?", map, map.expr );
let runNext = false;
ranOne = true;
handled = map.cb( req, res, ()=>(runNext = true) );
if( !runNext ) break;
}
}
if( cb = mappings.get( filepath ) ) {
//console.log( "got cb?" );
ranOne = true;
handled = cb( req, res, ()=>{} );
}
return handled;
}
}
}