1- const http = require ( "http" ) ;
1+ const fs = require ( "node:fs" ) ;
2+ const http = require ( "node:http" ) ;
3+ const path = require ( "node:path" ) ;
24
35const serveStatic = require ( "serve-static" ) ;
46const finalhandler = require ( "finalhandler" ) ;
@@ -30,7 +32,7 @@ function serve(
3032 if ( debug ) log ( `[srvd] waiting ${ wait } seconds for requests` ) ;
3133 const wait_ms = wait * 1000 ;
3234
33- const serve = serveStatic ( root , { acceptRanges } ) ;
35+ const _serve = serveStatic ( root , { acceptRanges } ) ;
3436
3537 let last = Date . now ( ) ;
3638 let server ;
@@ -73,7 +75,14 @@ function serve(
7375 count ++ ;
7476 last = Date . now ( ) ;
7577 if ( debug ) log ( `[srvd] received a "${ req . method } " request for "${ req . url } "` ) ;
76- serve ( req , res , finalhandler ( req , res ) ) ;
78+
79+ let filepath = path . join ( root , req . url ) ;
80+ if ( ! fs . existsSync ( filepath ) && fs . existsSync ( filepath + ".html" ) ) {
81+ if ( debug ) console . log ( `[srvd] inferred ${ req . url } .html` ) ;
82+ req . url += ".html" ;
83+ }
84+
85+ _serve ( req , res , finalhandler ( req , res ) ) ;
7786 if ( count >= max ) {
7887 if ( debug ) log ( "[srvd] reached maximum number of requests " + max ) ;
7988 server . close ( ) ;
@@ -86,6 +95,7 @@ function serve(
8695
8796 server . listen ( port ) ;
8897 if ( debug ) log ( "[srvd] serving on port " + port ) ;
98+ if ( debug ) log ( "[srvd] visit at http://localhost:" + port ) ;
8999
90100 checkWaitTimeout = setInterval ( checkWait , 500 ) ;
91101 checkForCloseTimeout = setInterval ( checkForCloseRequest , 500 ) ;
@@ -110,7 +120,7 @@ if (require.main === module) {
110120 const str = args . join ( " " ) ;
111121
112122 let wait = Array . prototype . slice . call ( str . match ( / - ? - w a i t (?: = | = = ) ( i n f ( i n i t y ) ? | \d + ) / i) || [ ] , 1 ) [ 0 ] ;
113- if ( wait ?. startsWith ( "inf" ) ) wait = Infinity ;
123+ if ( wait ?. toLowerCase ( ) . startsWith ( "inf" ) ) wait = Infinity ;
114124
115125 serve ( {
116126 debug : ! ! str . match ( / - ? - d e b u g ( ( = | = = ) ( t r u e | T r u e | T R U E ) ) ? / ) ,
0 commit comments