Skip to content

Instantly share code, notes, and snippets.

@ichsanputr
Created July 24, 2025 11:08
Show Gist options
  • Select an option

  • Save ichsanputr/574dbbdb9d5149e807f6d55ce1785549 to your computer and use it in GitHub Desktop.

Select an option

Save ichsanputr/574dbbdb9d5149e807f6d55ce1785549 to your computer and use it in GitHub Desktop.
const express = require('express');
const fs = require('fs');
const path = require('path');
const os = require('os');
const app = express();
const PORT = 4000;
// Target log directory (in home dir)
const logDir = path.join(os.homedir(), 'irandra-api', 'log');
app.get('/', (req, res) => {
fs.readdir(logDir, (err, files) => {
if (err) {
return res.status(500).send('Cannot read log directory');
}
const selectedFile = req.query.file;
let fileContent = '';
if (selectedFile && files.includes(selectedFile)) {
const filePath = path.join(logDir, selectedFile);
try {
const data = fs.readFileSync(filePath, 'utf8');
fileContent = `<h2>Contents of: ${selectedFile}</h2><pre>${data.replace(/</g, '&lt;')}</pre>`;
} catch (e) {
fileContent = `<p style="color:red;">Error reading file: ${selectedFile}</p>`;
}
}
const fileLinks = files.map(f =>
`<li><a href="/?file=${encodeURIComponent(f)}">${f}</a></li>`
).join('');
res.send(`
<h1>Log Viewer - ~/irandra-api/log</h1>
<ul>${fileLinks}</ul>
${fileContent}
`);
});
});
app.listen(PORT, () => {
console.log(`Log viewer running at http://localhost:${PORT}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment