1- import http from "http" ;
2- import fs from "fs" ;
3- import path from " path" ;
1+ import fs from 'fs' ;
2+ import http from 'http' ;
3+ import path from ' path' ;
44
55const PORT = 3001 ;
66
@@ -9,82 +9,70 @@ interface MimeTypes {
99}
1010
1111const MIME_TYPES : MimeTypes = {
12- " .html" : " text/html" ,
13- " .js" : " text/javascript" ,
14- " .css" : " text/css" ,
15- " .json" : " application/json" ,
16- " .png" : " image/png" ,
17- " .jpg" : " image/jpg" ,
18- " .gif" : " image/gif" ,
19- " .svg" : " image/svg+xml" ,
20- " .ico" : " image/x-icon" ,
12+ ' .html' : ' text/html' ,
13+ ' .js' : ' text/javascript' ,
14+ ' .css' : ' text/css' ,
15+ ' .json' : ' application/json' ,
16+ ' .png' : ' image/png' ,
17+ ' .jpg' : ' image/jpg' ,
18+ ' .gif' : ' image/gif' ,
19+ ' .svg' : ' image/svg+xml' ,
20+ ' .ico' : ' image/x-icon' ,
2121} ;
2222
2323const server = http . createServer ( ( req , res ) => {
24- console . log ( `${ req . method } ${ req . url } ` ) ;
25-
2624 if ( ! req . url ) {
2725 res . writeHead ( 400 ) ;
28- res . end ( " Bad request: URL missing" ) ;
26+ res . end ( ' Bad request: URL missing' ) ;
2927 return ;
3028 }
3129
32- // Handle the root path
3330 let filePath : string ;
34- if ( req . url === "/" ) {
35- filePath = path . join ( __dirname , "../public/dashboard.html" ) ;
36- } else if ( req . url === "/index.js" ) {
37- // Serve the main JS file from public folder
38- filePath = path . join ( __dirname , "../public/index.js" ) ;
39- } else if ( req . url === "/index.css" ) {
40- // Serve the main CSS file from public folder
41- filePath = path . join ( __dirname , "../public/index.css" ) ;
42- } else if ( req . url . startsWith ( "/metrics/" ) ) {
43- // Serve metrics files from the metrics directory
31+ if ( req . url === '/' ) {
32+ filePath = path . join ( __dirname , '../public/dashboard.html' ) ;
33+ } else if ( req . url === '/index.js' ) {
34+ filePath = path . join ( __dirname , '../public/index.js' ) ;
35+ } else if ( req . url === '/index.css' ) {
36+ filePath = path . join ( __dirname , '../public/index.css' ) ;
37+ } else if ( req . url . startsWith ( '/metrics/' ) ) {
4438 const metricsPath =
45- req . url === " /metrics/summary.json"
46- ? path . join ( __dirname , " ../metrics" , req . url . replace ( " /metrics/" , "" ) )
39+ req . url === ' /metrics/summary.json'
40+ ? path . join ( __dirname , ' ../metrics' , req . url . replace ( ' /metrics/' , '' ) )
4741 : path . join (
4842 __dirname ,
49- " ../metrics/tasks" ,
50- req . url . replace ( " /metrics/" , "" ) ,
43+ ' ../metrics/tasks' ,
44+ req . url . replace ( ' /metrics/' , '' ) ,
5145 ) ;
5246 filePath = metricsPath ;
53- } else if ( req . url . startsWith ( "/docs/" ) ) {
54- // Serve docs assets
55- filePath = path . join ( __dirname , "../public" , req . url ) ;
47+ } else if ( req . url . startsWith ( '/docs/' ) ) {
48+ filePath = path . join ( __dirname , '../public' , req . url ) ;
5649 } else {
57- // Serve other static files from the public directory
58- filePath = path . join ( __dirname , "../public" , req . url ) ;
50+ filePath = path . join ( __dirname , '../public' , req . url ) ;
5951 }
6052
6153 // Get the file extension
6254 const extname = path . extname ( filePath ) ;
63- const contentType = MIME_TYPES [ extname ] || " application/octet-stream" ;
55+ const contentType = MIME_TYPES [ extname ] || ' application/octet-stream' ;
6456
6557 // Read the file
6658 fs . readFile ( filePath , ( err , content ) => {
6759 if ( err ) {
68- if ( err . code === "ENOENT" ) {
69- // Page not found
70- console . error ( `File not found: ${ filePath } ` ) ;
60+ if ( err . code === 'ENOENT' ) {
7161 res . writeHead ( 404 ) ;
72- res . end ( " 404 Not Found" ) ;
62+ res . end ( ' 404 Not Found' ) ;
7363 } else {
74- // Server error
75- console . error ( `Server error: ${ err . code } ` ) ;
7664 res . writeHead ( 500 ) ;
7765 res . end ( `Server Error: ${ err . code } ` ) ;
7866 }
7967 } else {
8068 // Success
81- res . writeHead ( 200 , { " Content-Type" : contentType } ) ;
82- res . end ( content , " utf-8" ) ;
69+ res . writeHead ( 200 , { ' Content-Type' : contentType } ) ;
70+ res . end ( content , ' utf-8' ) ;
8371 }
8472 } ) ;
8573} ) ;
8674
8775server . listen ( PORT , ( ) => {
8876 console . log ( `Server running at http://localhost:${ PORT } /` ) ;
89- console . log ( " Press Ctrl+C to stop the server" ) ;
77+ console . log ( ' Press Ctrl+C to stop the server' ) ;
9078} ) ;
0 commit comments