@@ -18,20 +18,31 @@ http.createServer(function (request, response) {
18
18
var uri = url . parse ( request . url ) . pathname ,
19
19
filename = path . join ( __dirname , '..' , uri ) ;
20
20
21
- fs . exists ( filename , function ( exists ) {
22
- if ( ! exists ) {
23
- response . writeHead ( 404 , { 'Content-Type' : 'text/plain' } ) ;
24
- response . write ( '404 Not Found\n' ) ;
25
- response . end ( ) ;
26
- return ;
27
- }
21
+ if ( ! fs . lstatSync ( filename ) . isDirectory ( ) ) {
22
+ fs . exists ( filename , function ( exists ) {
23
+ if ( ! exists ) {
24
+ response . writeHead ( 404 , { 'Content-Type' : 'text/plain' } ) ;
25
+ response . write ( '404 Not Found\n' ) ;
26
+ response . end ( ) ;
27
+ return ;
28
+ }
28
29
29
- var type = filename . split ( '.' ) ;
30
- type = type [ type . length - 1 ] ;
30
+ var type = filename . split ( '.' ) ;
31
+ type = type [ type . length - 1 ] ;
31
32
32
- response . writeHead ( 200 , { 'Content-Type' : types [ type ] + '; charset=utf-8' } ) ;
33
- fs . createReadStream ( filename ) . pipe ( response ) ;
34
- } ) ;
33
+ response . writeHead ( 200 , { 'Content-Type' : types [ type ] + '; charset=utf-8' } ) ;
34
+ fs . createReadStream ( filename ) . pipe ( response ) ;
35
+ } ) ;
36
+ } else {
37
+ /**
38
+ * if users visit the site such as http://localhost:8888
39
+ * then lead them to http://localhost:8888/www/app.html
40
+ *
41
+ */
42
+ response . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ;
43
+ response . write ( 'Please visit <a href="www/app.html">here</a>\n' ) ;
44
+ response . end ( ) ;
45
+ }
35
46
} ) . listen ( parseInt ( port , 10 ) ) ;
36
47
37
48
console . log ( 'Static file server running at\n => http://localhost:' + port + '/\nCTRL + C to shutdown' ) ;
0 commit comments