@@ -16,37 +16,40 @@ var path = require('node:path');
16
16
var redis = require ( 'redis' ) ;
17
17
18
18
var db = redis . createClient ( ) ;
19
-
20
- // npm install redis
21
-
22
- // connect to Redis
23
-
24
- db . connect ( )
25
- . catch ( ( err ) => console . error ( 'Redis connection error:' , err ) ) ;
26
-
27
19
var app = express ( ) ;
28
20
29
21
app . use ( express . static ( path . join ( __dirname , 'public' ) ) ) ;
30
22
31
- // populate search
23
+ // npm install redis
32
24
33
- ( async ( ) => {
25
+ /**
26
+ * Redis Initialization
27
+ */
28
+
29
+ async function initializeRedis ( ) {
34
30
try {
31
+ // connect to Redis
32
+
33
+ await db . connect ( ) ;
34
+
35
+ // populate search
36
+
35
37
await db . sAdd ( 'ferret' , 'tobi' ) ;
36
38
await db . sAdd ( 'ferret' , 'loki' ) ;
37
39
await db . sAdd ( 'ferret' , 'jane' ) ;
38
40
await db . sAdd ( 'cat' , 'manny' ) ;
39
41
await db . sAdd ( 'cat' , 'luna' ) ;
40
42
} catch ( err ) {
41
- console . error ( 'Error populating Redis:' , err ) ;
43
+ console . error ( 'Error initializing Redis:' , err ) ;
44
+ process . exit ( 1 ) ;
42
45
}
43
- } ) ( ) ;
46
+ }
44
47
45
48
/**
46
49
* GET search for :query.
47
50
*/
48
51
49
- app . get ( '/search/:query{0,1 }' , function ( req , res , next ) {
52
+ app . get ( '/search/{ :query}' , function ( req , res , next ) {
50
53
var query = req . params . query || '' ;
51
54
db . sMembers ( query )
52
55
. then ( ( vals ) => res . send ( vals ) )
@@ -67,8 +70,15 @@ app.get('/client.js', function(req, res){
67
70
res . sendFile ( path . join ( __dirname , 'client.js' ) ) ;
68
71
} ) ;
69
72
70
- /* istanbul ignore next */
71
- if ( ! module . parent ) {
72
- app . listen ( 3000 ) ;
73
- console . log ( 'Express started on port 3000' ) ;
74
- }
73
+ /**
74
+ * Start the Server
75
+ */
76
+
77
+ ( async ( ) => {
78
+ await initializeRedis ( ) ;
79
+ if ( ! module . parent ) {
80
+ app . listen ( 3000 , ( ) => {
81
+ console . log ( 'Express started on port 3000' ) ;
82
+ } ) ;
83
+ }
84
+ } ) ( ) ;
0 commit comments