-
-
Save ichsanputr/574dbbdb9d5149e807f6d55ce1785549 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, '<')}</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