File tree Expand file tree Collapse file tree 1 file changed +14
-20
lines changed
blog/25-07-24/http-server/ex Expand file tree Collapse file tree 1 file changed +14
-20
lines changed Original file line number Diff line number Diff line change 66 console . log ( `http://localhost:${ port } /` ) ;
77 function request ( req , res ) {
88 console . log ( req . method , req . url ) ;
9- var url = req . url . slice ( 1 ) ;
10-
11- if ( url == '' ) {
12- res . writeHead ( 200 , { 'content-type' :'text/html' } ) . end ( html ) ;
13- return ;
14- }
15-
16- var ext = url . split ( '.' ) . at ( - 1 ) , type ;
17- switch ( ext ) {
18-
19- case 'html' : type = 'text/html' ;
20- case 'js' : type = 'text/javascript' ; break ;
21- case 'css' : type = 'text/css' ; break ;
22-
23- } //switch
9+ var path = req . url . slice ( 1 ) ;
10+ var type = mime ( path ) ;
2411
2512 res . writeHead ( 200 , { 'content-type' :type } ) ;
2613 require ( 'fs' ) . createReadStream ( url ) . pipe ( res ) ;
2714
2815 } //request
2916
17+ function mime ( path ) {
3018
31- var html = `
32- <style>html{font-family:arial}body{margin:20px}</style>
33- <h3>It Works!</h3>
34- ` ;
35-
19+ var ext = path . split ( '.' ) . at ( - 1 ) , type ;
20+ switch ( ext ) {
21+
22+ case 'html' : return 'text/html' ;
23+ case 'js' : return 'text/javascript' ;
24+ case 'css' : return 'text/css' ;
25+
26+ } //switch
27+ return 'application/octet-stream' ;
28+
29+ } //mime
3630
3731
3832
You can’t perform that action at this time.
0 commit comments