Update file-server.js

This commit is contained in:
ADARSH 2025-07-05 16:46:25 +05:30 committed by GitHub
parent 16ba6b2231
commit fc19f00b1b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -17,11 +17,15 @@ function getAllMarkdownFiles(dirPath, arrayOfFiles = []) {
if (stat.isDirectory()) {
getAllMarkdownFiles(fullPath, arrayOfFiles);
} else if (file.endsWith('.md')) {
const stats = fs.statSync(fullPath);
arrayOfFiles.push({
name: file,
path: fullPath,
relativePath: path.relative('.', fullPath),
folder: path.dirname(path.relative('.', fullPath))
folder: path.dirname(path.relative('.', fullPath)),
size: stats.size,
type: 'md',
lastModified: stats.mtime
});
}
} catch (error) {
@ -35,16 +39,6 @@ function getAllMarkdownFiles(dirPath, arrayOfFiles = []) {
return arrayOfFiles;
}
// Function to read file content
function readFileContent(filePath) {
try {
return fs.readFileSync(filePath, 'utf8');
} catch (error) {
console.error('Error reading file:', error);
return null;
}
}
// Get all markdown files
const markdownFiles = getAllMarkdownFiles('./');
@ -62,7 +56,7 @@ const server = http.createServer((req, res) => {
if (pathname.startsWith('/api/file')) {
const filePath = parsedUrl.query.path;
if (!filePath) {
res.writeHead(400);
res.writeHead(400, { 'Content-Type': 'text/plain' });
res.end('Missing file path');
return;
}
@ -72,7 +66,7 @@ const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
res.end(content);
} catch (error) {
res.writeHead(404);
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('File not found');
}
return;
@ -87,22 +81,63 @@ const server = http.createServer((req, res) => {
// Handle static files
let filePath = path.join(__dirname, pathname === '/' ? 'index.html' : pathname);
const extname = path.extname(filePath);
// Security check to prevent directory traversal
if (!filePath.startsWith(__dirname)) {
res.writeHead(403, { 'Content-Type': 'text/plain' });
res.end('Forbidden');
return;
}
const extname = path.extname(filePath).toLowerCase();
let contentType = 'text/html';
switch (extname) {
case '.css': contentType = 'text/css'; break;
case '.js': contentType = 'text/javascript'; break;
case '.json': contentType = 'application/json'; break;
case '.png': contentType = 'image/png'; break;
case '.jpg': contentType = 'image/jpg'; break;
case '.ico': contentType = 'image/x-icon'; break;
case '.css':
contentType = 'text/css';
break;
case '.js':
contentType = 'text/javascript';
break;
case '.json':
contentType = 'application/json';
break;
case '.png':
contentType = 'image/png';
break;
case '.jpg':
case '.jpeg':
contentType = 'image/jpeg';
break;
case '.ico':
contentType = 'image/x-icon';
break;
case '.svg':
contentType = 'image/svg+xml';
break;
case '.woff':
contentType = 'font/woff';
break;
case '.woff2':
contentType = 'font/woff2';
break;
case '.ttf':
contentType = 'font/ttf';
break;
case '.eot':
contentType = 'application/vnd.ms-fontobject';
break;
}
fs.readFile(filePath, (err, content) => {
if (err) {
res.writeHead(404);
res.end('Not found');
if (err.code === 'ENOENT') {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('File not found');
} else {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('Server error');
}
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content);