@@ -18,20 +18,31 @@ http.createServer(function (request, response) {
1818 var uri = url . parse ( request . url ) . pathname ,
1919 filename = path . join ( __dirname , '..' , uri ) ;
2020
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+ }
2829
29- var type = filename . split ( '.' ) ;
30- type = type [ type . length - 1 ] ;
30+ var type = filename . split ( '.' ) ;
31+ type = type [ type . length - 1 ] ;
3132
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+ }
3546} ) . listen ( parseInt ( port , 10 ) ) ;
3647
3748console . log ( 'Static file server running at\n => http://localhost:' + port + '/\nCTRL + C to shutdown' ) ;
0 commit comments