0

I'm working on code which will display log files on my computer to a window with a clickable link to access the contents of the log file. The code shown below is script.js and index.html. srcipt.js reads the files and uses a for loop to format them in html and index.html is just supposed to display the logs to the window. However whenever I press my button all it displays is the word undefined on my window and I'm not sure why.

script.js

const fs = require('fs');

var outputHTML = '';

var logdirname = '.././logs/';
    var alllogs = fs.readdirSync(logdirname, function(err, filenames) {
        if (err) {
            onError(err);
            return console.log("Error");
            }
    });

outputHTML += "<table>";
for (var i = 0; i <= alllogs.length ; i++) {
    let iPlusOne = i+1;
    outputHTML += "<tr><td>";
    outputHTML += "<a href=G:\logs\\" + alllogs[i] + ">Log File " + iPlusOne;
    outputHTML += "</a></td></tr>\n"
}
outputHTML += "</table>";
console.log(outputHTML);

index.html

<!DOCTYPE html>
<html>

<head>
    <script type="text/javascript"
        src="script.js">
    </script>
    
</head>

<body>
    <button onclick= "f()">
        Click To Access Logs
    </button>

    <div>
        <p id="text">
        </p>
    </div>
    
    <script type="text/javascript">
        

        function f() {
            
           document.getElementById("text").innerHTML = outputHTML;

        }

    </script>
</body>
</html>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Ewgavin
  • 13
  • 2
  • Why not just render it inside the DIV? A table is not valid HTML inside a P tag – mplungjan Jun 28 '22 at 10:53
  • @mplungjan the script is included in the page – VLAZ Jun 28 '22 at 10:53
  • Client side JS cannot require and use server side FS – mplungjan Jun 28 '22 at 11:11
  • @mplungjan what is the work around for this? I don't really understand why but if I put all code in script tags I get an error saying fs won't work and if I put all my code in .js I get an error saying document isn't defined. This is the only way I get no errors but I'm still unsure of what the work around to my current problem is. – Ewgavin Jun 28 '22 at 13:38
  • https://stackoverflow.com/questions/17618655/how-to-read-text-file-and-pipe-to-html-table-using-node-js-and-express – mplungjan Jun 28 '22 at 13:44
  • @mplungjan I have used this code and I get a similar response in this window. All it returns is "Cannot GET /", could there be a problem with readdirSync? – Ewgavin Jun 29 '22 at 06:47
  • I have no idea. I cannot guess your setup and do not use node much – mplungjan Jun 29 '22 at 06:48

0 Answers0