@@ -9,37 +9,38 @@ import { extname, join } from 'node:path';
99import { fileURLToPath } from 'node:url' ;
1010import ansiColors from 'ansi-colors' ;
1111
12- const pathDir = fileURLToPath ( new URL ( '..' , import . meta. url ) ) ;
12+ const baseDir = fileURLToPath ( new URL ( '..' , import . meta. url ) ) ;
1313const mimeTypes =
1414{
1515 '.css' : 'text/css' ,
1616 '.html' : 'text/html' ,
1717 '.js' : 'application/javascript' ,
18+ '.json' : 'application/json' ,
1819 '.mjs' : 'application/javascript' ,
1920} ;
2021const port = 8080 ;
2122createServer
2223(
2324 ( { url } , response ) =>
2425 {
25- const requestPath = fileURLToPath ( new URL ( url , 'file:' ) ) ;
26- if ( requestPath === '/favicon.ico' )
26+ const { pathname } = new URL ( url , 'file:' ) ;
27+ if ( pathname === '/favicon.ico' )
2728 {
2829 const headers = { 'Content-Type' : 'image/x-icon' } ;
2930 response . writeHead ( 204 , headers ) ;
3031 response . end ( ) ;
3132 return ;
3233 }
33- const pathname = join ( pathDir , requestPath ) ;
34- const stream = createReadStream ( pathname ) ;
34+ const fullPath = join ( baseDir , pathname ) ;
35+ const stream = createReadStream ( fullPath ) ;
3536 stream . on
3637 (
3738 'open' ,
3839 ( ) =>
3940 {
4041 const headers = { } ;
4142 {
42- const ext = extname ( requestPath ) ;
43+ const ext = extname ( fullPath ) ;
4344 if ( mimeTypes . hasOwnProperty ( ext ) )
4445 headers [ 'Content-Type' ] = mimeTypes [ ext ] ;
4546 }
0 commit comments