mirror of
https://github.com/tenrok/axios.git
synced 2026-05-30 15:24:11 +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:
+93
-42
@@ -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('<h1>404 Not Found</h1>');
|
||||
case '/api':
|
||||
handleApiRequest(req, res);
|
||||
break;
|
||||
|
||||
default:
|
||||
res.writeHead(404);
|
||||
res.end('<h1>404 Not Found</h1>');
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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) => {
|
||||
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();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user