File tree Expand file tree Collapse file tree 1 file changed +7
-13
lines changed Expand file tree Collapse file tree 1 file changed +7
-13
lines changed Original file line number Diff line number Diff line change @@ -8,20 +8,14 @@ const users = {
8
8
} ;
9
9
10
10
const routing = {
11
- '/api/user/' : name => users [ name ] ,
12
- '/api/userBorn/' : name => users [ name ] . born
13
- } ;
14
-
15
- const types = {
16
- object : o => JSON . stringify ( o ) ,
17
- undefined : ( ) => '{"error":"not found"}' ,
18
- function : ( fn , req , res ) => JSON . stringify ( fn ( req , res ) )
11
+ '/api/user' : name => users [ name ] ,
12
+ '/api/userBorn' : name => users [ name ] . born
19
13
} ;
20
14
21
15
http . createServer ( ( req , res ) => {
22
- const data = routing [ req . url ] ;
23
- const type = typeof ( data ) ;
24
- const serializer = types [ type ] ;
25
- const result = serializer ( data , req , res ) ;
26
- res . end ( result ) ;
16
+ const url = req . url . split ( '/' ) ;
17
+ const par = url . pop ( ) ;
18
+ const method = routing [ url . join ( '/' ) ] ;
19
+ const result = method ? method ( par ) : { error : 'not found' } ;
20
+ res . end ( JSON . stringify ( result ) ) ;
27
21
} ) . listen ( 8000 ) ;
You can’t perform that action at this time.
0 commit comments