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
+77 -26
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,37 +19,28 @@ 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) { /**
req.setEncoding('utf8'); * Handles API requests to /api.
*
const parsed = url.parse(req.url, true); * Collects request data, parses it as JSON, and returns a JSON response
let pathname = parsed.pathname; * containing the request URL, method, headers, and parsed data.
*
console.log('[' + new Date() + ']', req.method, pathname); * @param {http.IncomingMessage} req - The HTTP request object.
* @param {http.ServerResponse} res - The HTTP response object.
if (pathname === '/') { */
pathname = '/index.html'; function handleApiRequest(req, res) {
}
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 status;
let result; let result;
let data = ''; let data = '';
req.on('data', function (chunk) { req.on('data', (chunk) => {
data += chunk; data += chunk;
}); });
req.on('end', function () { req.on('end', () => {
try { try {
status = 200; status = 200;
result = { result = {
@@ -63,18 +62,70 @@ server = http.createServer(function (req, res) {
}); });
res.end(JSON.stringify(result)); res.end(JSON.stringify(result));
}); });
} else { }
/**
* 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);
let pathname = parsed.pathname;
console.log('[' + new Date() + ']', req.method, pathname);
if (pathname === '/') {
pathname = '/index.html';
}
switch (pathname) {
case '/index.html':
pipeFileToResponse(res, './client.html', 'text/html');
break;
case '/axios.js':
pipeFileToResponse(res, '../dist/axios.js', 'text/javascript');
break;
case '/axios.js.map':
pipeFileToResponse(res, '../dist/axios.js.map', 'text/javascript');
break;
case '/api':
handleApiRequest(req, res);
break;
default:
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();
} }
}); });