Genesis
This commit is contained in:
44
server.js
Normal file
44
server.js
Normal file
@@ -0,0 +1,44 @@
|
||||
const express = require('express');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const app = express();
|
||||
const PORT = 3000;
|
||||
const DIRECTORY = path.join(__dirname, 'tables'); // Change to your directory
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
fs.readdir(DIRECTORY, (err, files) => {
|
||||
if (err) return res.status(500).send('Error reading directory');
|
||||
let html = '<h1>File Index</h1><ul>';
|
||||
files.forEach(file => {
|
||||
html += `<li><a href="/file/${encodeURIComponent(file)}">${file}</a></li>`;
|
||||
});
|
||||
html += '</ul>';
|
||||
res.send(html);
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/file/:filename', (req, res) => {
|
||||
const filename = req.params.filename;
|
||||
const filepath = path.join(DIRECTORY, filename);
|
||||
fs.readFile(filepath, 'utf8', (err, data) => {
|
||||
if (err) return res.status(404).send('File not found');
|
||||
res.send(`<h1>${filename}</h1><pre>${escapeHtml(data)}</pre><a href="/">Back</a>`);
|
||||
});
|
||||
});
|
||||
|
||||
function escapeHtml(text) {
|
||||
return text.replace(/[&<>"']/g, function(m) {
|
||||
return ({
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": '''
|
||||
})[m];
|
||||
});
|
||||
}
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server running at http://localhost:${PORT}`);
|
||||
});
|
||||
Reference in New Issue
Block a user