* Handles incoming HTTP requests. * * Serves static files like index.html and axios.js, or routes API requests to * handleApiRequest. Responds with 404 for unrecognized paths. * * @param {http.IncomingMessage} req - The HTTP request object. * @param {http.ServerResponse} res - The HTTP respons
(req, res)
| 73 | * @param {http.ServerResponse} res - The HTTP response object. |
| 74 | */ |
| 75 | function requestHandler(req, res) { |
| 76 | req.setEncoding('utf8'); |
| 77 | |
| 78 | const parsed = new URL(req.url, 'http://localhost'); |
| 79 | const pathname = parsed.pathname; |
| 80 | |
| 81 | console.log('[' + new Date() + ']', req.method, pathname); |
| 82 | |
| 83 | if (pathname === '/') { |
| 84 | pathname = '/index.html'; |
| 85 | } |
| 86 | |
| 87 | switch (pathname) { |
| 88 | case '/index.html': |
| 89 | pipeFileToResponse(res, './client.html', 'text/html'); |
| 90 | break; |
| 91 | |
| 92 | case '/axios.js': |
| 93 | pipeFileToResponse(res, '../dist/axios.js', 'text/javascript'); |
| 94 | break; |
| 95 | |
| 96 | case '/axios.js.map': |
| 97 | pipeFileToResponse(res, '../dist/axios.js.map', 'text/javascript'); |
| 98 | break; |
| 99 | |
| 100 | case '/api': |
| 101 | handleApiRequest(req, res); |
| 102 | break; |
| 103 | |
| 104 | default: |
| 105 | res.writeHead(404); |
| 106 | res.end('<h1>404 Not Found</h1>'); |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | const PORT = 3000; |
| 112 |
nothing calls this directly
no test coverage detected