diff --git a/sandbox/server.js b/sandbox/server.js index 3ee03de..8b5693f 100644 --- a/sandbox/server.js +++ b/sandbox/server.js @@ -2,8 +2,16 @@ import fs from 'fs'; import url from 'url'; import path from 'path'; import http from 'http'; + let server; +/** + * Pipes a file to the HTTP response. + * + * @param {http.ServerResponse} res - The HTTP response object. + * @param {string} file - The relative path to the file to be served. + * @param {string} [type] - Optional MIME type for the response. + */ function pipeFileToResponse(res, file, type) { if (type) { res.writeHead(200, { @@ -11,10 +19,61 @@ function pipeFileToResponse(res, file, type) { }); } - fs.createReadStream(path.join(path.resolve() ,'sandbox', file)).pipe(res); + fs.createReadStream(path.join(path.resolve(), 'sandbox', file)).pipe(res); } -server = http.createServer(function (req, res) { +/** + * Handles API requests to /api. + * + * Collects request data, parses it as JSON, and returns a JSON response + * containing the request URL, method, headers, and parsed data. + * + * @param {http.IncomingMessage} req - The HTTP request object. + * @param {http.ServerResponse} res - The HTTP response object. + */ +function handleApiRequest(req, res) { + let status; + let result; + let data = ''; + + req.on('data', (chunk) => { + data += chunk; + }); + + req.on('end', () => { + try { + status = 200; + result = { + url: req.url, + data: data ? JSON.parse(data) : undefined, + method: req.method, + headers: req.headers + }; + } catch (e) { + console.error('Error:', e.message); + status = 400; + result = { + error: e.message + }; + } + + res.writeHead(status, { + 'Content-Type': 'application/json' + }); + res.end(JSON.stringify(result)); + }); +} + +/** + * 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 response object. + */ +function requestHandler(req, res) { req.setEncoding('utf8'); const parsed = url.parse(req.url, true); @@ -26,55 +85,47 @@ server = http.createServer(function (req, res) { pathname = '/index.html'; } - if (pathname === '/index.html') { - pipeFileToResponse(res, './client.html', 'text/html'); - } else if (pathname === '/axios.js') { - pipeFileToResponse(res, '../dist/axios.js', 'text/javascript'); - } else if (pathname === '/axios.js.map') { - pipeFileToResponse(res, '../dist/axios.js.map', 'text/javascript'); - } else if (pathname === '/api') { - let status; - let result; - let data = ''; + switch (pathname) { + case '/index.html': + pipeFileToResponse(res, './client.html', 'text/html'); + break; - req.on('data', function (chunk) { - data += chunk; - }); + case '/axios.js': + pipeFileToResponse(res, '../dist/axios.js', 'text/javascript'); + break; - req.on('end', function () { - try { - status = 200; - result = { - url: req.url, - data: data ? JSON.parse(data) : undefined, - method: req.method, - headers: req.headers - }; - } catch (e) { - console.error('Error:', e.message); - status = 400; - result = { - error: e.message - }; - } + case '/axios.js.map': + pipeFileToResponse(res, '../dist/axios.js.map', 'text/javascript'); + break; - res.writeHead(status, { - 'Content-Type': 'application/json' - }); - res.end(JSON.stringify(result)); - }); - } else { - res.writeHead(404); - res.end('