Skip to content

Commit 78269da

Browse files
committed
show directory file listings
1 parent f841840 commit 78269da

File tree

1 file changed

+91
-38
lines changed

1 file changed

+91
-38
lines changed

server.js

Lines changed: 91 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,104 @@ const server = http.createServer((req, res) => {
1919
// Default content type
2020
let contentType = 'text/html';
2121

22-
// Check ext and set content type
23-
switch (extname) {
24-
case '.js':
25-
contentType = 'text/javascript';
26-
break;
27-
case '.css':
28-
contentType = 'text/css';
29-
break;
30-
case '.json':
31-
contentType = 'application/json';
32-
break;
33-
case '.png':
34-
contentType = 'image/png';
35-
break;
36-
case '.jpg':
37-
contentType = 'image/jpg';
38-
break;
39-
case '.yaml':
40-
contentType = 'text/yaml';
41-
break;
42-
case '.yml':
43-
contentType = 'text/yaml';
44-
break;
45-
}
46-
47-
// Read file
48-
fs.readFile(filePath, (err, content) => {
22+
// Check if path is a directory
23+
fs.stat(filePath, (err, stats) => {
4924
if (err) {
50-
if (err.code == 'ENOENT') {
51-
// Page not found
25+
res.writeHead(404);
26+
res.end('Not Found');
27+
return;
28+
}
29+
30+
if (stats.isDirectory()) {
31+
// Don't show hidden directories
32+
if (path.basename(filePath).startsWith('.')) {
5233
res.writeHead(404);
53-
res.end(`Not Found`);
54-
} else {
55-
// Some server error
56-
res.writeHead(500);
57-
res.end(`Server Error: ${err.code}`);
34+
res.end('Not Found');
35+
return;
5836
}
37+
38+
fs.readdir(filePath, (err, files) => {
39+
if (err) {
40+
res.writeHead(404);
41+
res.end('Not Found');
42+
return;
43+
}
44+
45+
// Filter out hidden files
46+
files = files.filter(file => !file.startsWith('.'));
47+
48+
// Create HTML directory listing
49+
const html = `
50+
<!DOCTYPE html>
51+
<html>
52+
<head>
53+
<title>Directory listing for ${req.url}</title>
54+
<style>
55+
body { font-family: Arial, sans-serif; margin: 20px; }
56+
ul { list-style-type: none; padding: 0; }
57+
li { margin: 5px 0; }
58+
a { text-decoration: none; color: #0366d6; }
59+
a:hover { text-decoration: underline; }
60+
</style>
61+
</head>
62+
<body>
63+
<h1>Directory listing for ${req.url}</h1>
64+
<ul>
65+
${req.url !== '/' ? '<li><a href="..">..</a></li>' : ''}
66+
${files.map(file => `<li><a href="${path.join(req.url, file)}">${file}</a></li>`).join('\n')}
67+
</ul>
68+
</body>
69+
</html>
70+
`;
71+
res.writeHead(200, { 'Content-Type': 'text/html' });
72+
res.end(html);
73+
return;
74+
});
5975
} else {
60-
// Success
61-
res.writeHead(200, { 'Content-Type': contentType });
62-
res.end(content, 'utf8');
76+
// Check ext and set content type
77+
switch (extname) {
78+
case '.js':
79+
contentType = 'text/javascript';
80+
break;
81+
case '.css':
82+
contentType = 'text/css';
83+
break;
84+
case '.json':
85+
contentType = 'application/json';
86+
break;
87+
case '.png':
88+
contentType = 'image/png';
89+
break;
90+
case '.jpg':
91+
contentType = 'image/jpg';
92+
break;
93+
case '.yaml':
94+
contentType = 'text/yaml';
95+
break;
96+
case '.yml':
97+
contentType = 'text/yaml';
98+
break;
99+
}
100+
101+
// Read file
102+
fs.readFile(filePath, (err, content) => {
103+
if (err) {
104+
if (err.code == 'ENOENT') {
105+
res.writeHead(404);
106+
res.end(`Not Found`);
107+
} else {
108+
res.writeHead(500);
109+
res.end(`Server Error: ${err.code}`);
110+
}
111+
} else {
112+
res.writeHead(200, { 'Content-Type': contentType });
113+
res.end(content, 'utf8');
114+
}
115+
});
63116
}
64117
});
65118
});
66119

67120
const PORT = process.env.PORT || 3000;
68121

69-
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
122+
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));

0 commit comments

Comments
 (0)