@@ -5,6 +5,31 @@ const WebSocketResponse = require('./websocket')
5
5
const Connection = require ( './connection' )
6
6
const ServerError = require ( './errors' )
7
7
8
+ async function handler ( params , callbacks , response , request ) {
9
+ const paramsMap = { }
10
+ if ( params ) {
11
+ params . forEach ( ( key , index ) => {
12
+ paramsMap [ key ] = decodeURIComponent ( request . getParameter ( index ) )
13
+ } )
14
+ }
15
+ const conn = Connection . create ( this , request , response )
16
+ const req = Request . create ( conn )
17
+ const res = Response . create ( conn )
18
+ try {
19
+ conn . processBodyData ( )
20
+ await callbacks ( req , res , paramsMap )
21
+ } catch ( e ) {
22
+ this . log . error ( 'Server Internal Error' , e )
23
+ if ( ! res . _writableState . destroyed ) {
24
+ if ( e instanceof ServerError && e . httpCode ) {
25
+ res . status ( e . httpCode ) . end ( e . message )
26
+ } else {
27
+ res . status ( 500 ) . end ( 'Server Internal Error' )
28
+ }
29
+ }
30
+ }
31
+ }
32
+
8
33
class Routes {
9
34
constructor ( ) {
10
35
this . _routes = { }
@@ -24,51 +49,25 @@ class Routes {
24
49
25
50
route ( method , path , callbacks ) {
26
51
if ( this . lock ) {
27
- throw new ServerError ( {
28
- code : 'SERVER_ALREADY_STARTED' ,
29
- message : 'Cannot add route after sterver started.'
30
- } )
52
+ throw new Error ( 'The server is started, should not add route.' )
53
+ }
54
+ if ( typeof path !== 'string' && ! path . startsWith ( '/' ) ) {
55
+ throw new TypeError ( 'Route path whould starts with "/".' )
31
56
}
32
57
if ( ! this . _routes [ path ] ) {
33
58
this . _routes [ path ] = { }
34
59
}
35
60
if ( this . _routes [ path ] [ method ] ) {
36
- throw new ServerError ( {
37
- code : 'INVALID_DUPLICATE_ROUTER' ,
38
- message : 'Invalid, duplicated router.'
39
- } )
61
+ throw new Error ( `${ method . toUpperCase ( ) } "${ path } " is exists.` )
40
62
}
41
63
if ( method === 'ws' ) {
42
64
this . _routes [ path ] [ method ] = callbacks
43
65
} else {
44
- let URLParams = path . match ( / : \w + / g)
45
- if ( URLParams ) {
46
- URLParams = URLParams . map ( key => key . slice ( 1 ) )
47
- }
48
- this . _routes [ path ] [ method ] = async ( response , request ) => {
49
- const params = { }
50
- if ( URLParams ) {
51
- URLParams . forEach ( ( key , index ) => {
52
- params [ key ] = decodeURIComponent ( request . getParameter ( index ) )
53
- } )
54
- }
55
- const conn = Connection . create ( this , request , response )
56
- const req = Request . create ( conn )
57
- const res = Response . create ( conn )
58
- try {
59
- conn . processBodyData ( )
60
- await callbacks ( req , res , params )
61
- } catch ( e ) {
62
- this . log . error ( 'Server Internal Error' , e )
63
- if ( ! res . _writableState . destroyed ) {
64
- if ( e instanceof ServerError && e . httpCode ) {
65
- res . status ( e . httpCode ) . end ( e . message )
66
- } else {
67
- res . status ( 500 ) . end ( 'Server Internal Error' )
68
- }
69
- }
70
- }
66
+ let params = path . match ( / : \w + / g)
67
+ if ( params ) {
68
+ params = params . map ( key => key . slice ( 1 ) )
71
69
}
70
+ this . _routes [ path ] [ method ] = handler . bind ( this , params , callbacks )
72
71
}
73
72
}
74
73
0 commit comments