@@ -12,7 +12,9 @@ module.exports = (config = {}) => {
12
12
res . statusCode = 500
13
13
res . end ( err . message )
14
14
} )
15
- config . cacheSize = config . cacheSize || 1000
15
+ if ( config . cacheSize === undefined ) {
16
+ config . cacheSize = 1000
17
+ }
16
18
config . id = config . id || ( Date . now ( ) . toString ( 36 ) + Math . random ( ) . toString ( 36 ) . substr ( 2 , 5 ) ) . toUpperCase ( )
17
19
18
20
const routers = { }
@@ -39,15 +41,29 @@ module.exports = (config = {}) => {
39
41
}
40
42
41
43
router . lookup = ( req , res , step ) => {
42
- req . url = req . url || '/'
43
- req . originalUrl = req . originalUrl || req . url
44
- req . path = req . url . split ( '?' ) [ 0 ]
44
+ if ( ! req . url ) {
45
+ req . url = '/'
46
+ }
47
+ if ( ! req . originalUrl ) {
48
+ req . originalUrl = req . url
49
+ }
50
+ const varsIndex = req . url . indexOf ( '?' )
51
+ if ( varsIndex > 0 ) {
52
+ req . path = req . url . slice ( 0 , varsIndex )
53
+ } else {
54
+ req . path = req . url
55
+ }
45
56
46
- const reqCacheKey = `${ req . method + req . path } `
47
- let match = cache . get ( reqCacheKey )
48
- if ( ! match ) {
57
+ let match
58
+ if ( config . cacheSize > 0 ) {
59
+ const reqCacheKey = `${ req . method + req . path } `
60
+ match = cache . get ( reqCacheKey )
61
+ if ( ! match ) {
62
+ match = router . find ( req . method , req . path )
63
+ cache . set ( reqCacheKey , match )
64
+ }
65
+ } else {
49
66
match = router . find ( req . method , req . path )
50
- cache . set ( reqCacheKey , match )
51
67
}
52
68
53
69
if ( match . handlers . length ) {
@@ -66,7 +82,10 @@ module.exports = (config = {}) => {
66
82
}
67
83
68
84
// middlewares invocation
69
- req . params = Object . assign ( req . params || { } , match . params )
85
+ if ( ! req . params ) {
86
+ req . params = { }
87
+ }
88
+ req . params = Object . assign ( req . params , match . params )
70
89
71
90
return next ( middlewares , req , res , 0 , routers , config . defaultRoute , config . errorHandler )
72
91
} else {
0 commit comments