2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-20 20:00:40 +03:00

docs: add JSDoc comments to server.js (#7135)

* add contributors and documentation

* docs(adapters): add JSDoc for adapters module

* docs: add JSDoc comments to server.js

---------

Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
Aviraj2929
2025-10-12 20:19:35 +05:30
committed by GitHub
parent 9b9ec98548
commit e1f8ae627c
+93 -42
View File
@@ -2,8 +2,16 @@ import fs from 'fs';
import url from 'url'; import url from 'url';
import path from 'path'; import path from 'path';
import http from 'http'; import http from 'http';
let server; 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) { function pipeFileToResponse(res, file, type) {
if (type) { if (type) {
res.writeHead(200, { 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'); req.setEncoding('utf8');
const parsed = url.parse(req.url, true); const parsed = url.parse(req.url, true);
@@ -26,55 +85,47 @@ server = http.createServer(function (req, res) {
pathname = '/index.html'; pathname = '/index.html';
} }
if (pathname === '/index.html') { switch (pathname) {
pipeFileToResponse(res, './client.html', 'text/html'); case '/index.html':
} else if (pathname === '/axios.js') { pipeFileToResponse(res, './client.html', 'text/html');
pipeFileToResponse(res, '../dist/axios.js', 'text/javascript'); break;
} else if (pathname === '/axios.js.map') {
pipeFileToResponse(res, '../dist/axios.js.map', 'text/javascript');
} else if (pathname === '/api') {
let status;
let result;
let data = '';
req.on('data', function (chunk) { case '/axios.js':
data += chunk; pipeFileToResponse(res, '../dist/axios.js', 'text/javascript');
}); break;
req.on('end', function () { case '/axios.js.map':
try { pipeFileToResponse(res, '../dist/axios.js.map', 'text/javascript');
status = 200; break;
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, { case '/api':
'Content-Type': 'application/json' handleApiRequest(req, res);
}); break;
res.end(JSON.stringify(result));
}); default:
} else { res.writeHead(404);
res.writeHead(404); res.end('<h1>404 Not Found</h1>');
res.end('<h1>404 Not Found</h1>'); break;
} }
}); }
const PORT = 3000; const PORT = 3000;
server.listen(PORT, console.log(`Listening on localhost:${PORT}...`)); // Create and start the HTTP server
server = http.createServer(requestHandler);
server.listen(PORT, () => {
console.log(`Listening on localhost:${PORT}...`);
});
/**
* Handles server errors, e.g., port already in use.
*
* @param {NodeJS.ErrnoException} error - The server error object.
*/
server.on('error', (error) => { server.on('error', (error) => {
if (error.code === 'EADDRINUSE') { if (error.code === 'EADDRINUSE') {
console.log(`Address localhost:${PORT} in use please retry when the port is available!`); console.log(`Address localhost:${PORT} in use. Please retry when the port is available!`);
server.close(); server.close();
} }
}); });