@@ -12,26 +12,36 @@ var http = require('http'),
12
12
types = {
13
13
'html' : 'text/html' ,
14
14
'js' : 'application/javascript'
15
- } ;
15
+ } ,
16
+ site = 'http://localhost:' + port ;
16
17
17
18
http . createServer ( function ( request , response ) {
18
19
var uri = url . parse ( request . url ) . pathname ,
19
20
filename = path . join ( __dirname , '..' , uri ) ;
20
21
21
22
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
- }
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
+ if ( ! fs . lstatSync ( filename ) . isDirectory ( ) ) {
31
+ var type = filename . split ( '.' ) ;
32
+ type = type [ type . length - 1 ] ;
31
33
32
- response . writeHead ( 200 , { 'Content-Type' : types [ type ] + '; charset=utf-8' } ) ;
33
- fs . createReadStream ( filename ) . pipe ( response ) ;
34
- } ) ;
34
+ response . writeHead ( 200 , { 'Content-Type' : types [ type ] + '; charset=utf-8' } ) ;
35
+ fs . createReadStream ( filename ) . pipe ( response ) ;
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
+ response . writeHead ( 301 , { 'Location' : site + '/www/app.html' } ) ;
42
+ response . end ( ) ;
43
+ }
44
+ } ) ;
35
45
} ) . listen ( parseInt ( port , 10 ) ) ;
36
46
37
- console . log ( 'Static file server running at\n => http://localhost: ' + port + '/\nCTRL + C to shutdown' ) ;
47
+ console . log ( 'Static file server running at\n => ' + site + '/\nCTRL + C to shutdown' ) ;
0 commit comments