Display contents of file on HTML page - javascript

I am looking for a way to display the contents of a directory on an HTML page. I have the following snippet of code:
const fs = require("fs");
let directory_name = "/example";
let filenames = fs.readdirSync(directory_name);
console.log("\nFilenames in directory:");
filenames.forEach((file) => {
console.log("File:", file);
});
This will display the contents of the file in the terminal(console) but I am looking for a way to change the "console.log("File:", file)" line into a statement to send the elements to my HTML page.
Any help would be appreciated.
Using Linux btw

Use the express.js res.send function.

Related

How to add html file into different js folder

pic
I want to add html file in my js, and this pic is the path in my directory. How can I do?
in my config.js, I widh to include my file as let ADDR_iframe1 = "../templates/switch.html";
Use JavaScript fetch() Method to Load an External HTML File.
Please find the below example for your reference.
function loadHTML() {
fetch('switch.html')
.then(response=> response.text())
.then(text=> document.getElementById('xyz').innerHTML = text);
}

Opening CSV file in Django using JavaScript

I'm starting to learn Django and one of the projects I have chosen for myself is one that has an HTML front page, in which the user inputs some search text and then that calls to an API using Python and Pytrends, then its saves that information as a CSV file.
I'm having a hard time with the next part which is importing that CSV file into another HTML page, where I would like to use my chart.js file to turn it into a graph. I can't figure out how to open up the file and run it inside of javascript. I have tried a static load but it's not opening up the file, just the path to the file. Please let me know if you need to see any other files or know anything else.
async function getData() {
const data = '{% static "interest.csv" %}';
console.log(data.text());
const table = data.split("\n").slice(1);
table.forEach((row) => {
const columns = row.split(",");
const year = columns[0];
const interest = columns[1];
xlabel.push(year);
console.log(year, interest);
});

Creating a PDF and moving it to a directory?

I am using PDFMake to create a PDF file, the body of which is dynamic. When I create the PDF it downloads to my downloads folder.
I would love for the PDF to be able to save to a directory of my choice. I have looked through the PDFMake docs and all I can see is that it gives you the option to give it a custom name. Does anyone know how I can make this happen. I don't want to just retrieve it from the folder it's in because it could possibly be in a different folder depending on the user.
Below is the code where I create the PDF:
function createPDF() {
let body = tinymce.get('elm1').getContent();
let docDefinition = {
content: body
};
console.log(body);
pdfMake.createPdf(docDefinition).download();
}
Example:
let fs = require('fs');
let PdfPrinter = require('pdfmake');
let printer = new PdfPrinter();
let docDefinition = {
content: .....
};
pdf = printer.createPdfKitDocument(docDefinition);
pdf.pipe(fs.createWriteStream('YOUR-PDFs/YOUR-PDF.pdf'));
pdf.end();

NodeJS - How to insert an element in a HTML document using Javascript?

I'm trying to use NodeJS to modify an external HTML file (which is located in the same directory). In my index.js file I write:
fs.readFile('index.html', (err,html)=>{
if(err){
throw err;
}
html.body.innerHTML += '<div id = "asdf"></div>';
});
As index.html is a valid document. But it doesn't look to be reading it properly, as I get as an error:
"TypeError: Cannot read property 'innerHTML' of undefined".
I guess that html is not getting anything as body.
How can I do changes in HTML using JavaScript?
Here is an example using node-html-parse
HTML file
<html>
<body>
<div id="fist">yolo</div>
</body>
</html>
And the nodejs
const fs = require('fs');
const parse = require('node-html-parser').parse;
fs.readFile('index.html', 'utf8', (err,html)=>{
if(err){
throw err;
}
const root = parse(html);
const body = root.querySelector('body');
//body.set_content('<div id = "asdf"></div>');
body.appendChild('<div id = "asdf"></div>');
console.log(root.toString()); // This you can write back to file!
});
There might be better solutions than node-html-parser, considering the amount of downloads. For example, htmlparser2 has much more downloads, but it also looks more complex :)
In order to manipulate an html file the way you'd be able to in a browser, you'll first need to parse it.
Perhaps node-html-parser can be of use? (Or if a few milliseconds of parsing are not a concern and you want some more functionality, the JSDOM package is very popular too.)
innerHTML is a function provided after DOM parsing. Here you are using a string, so you can either use a DOM parser to create the structure or you can just use regex to isolate the part you want to replace and append the text.
html.replace("</body>",'<div id = "asdf"></div></body>');

Dumping Source Code into a local file using CasperJS

When I use download() in CasperJS, I get a file saved in the system, but the file doesn't contain the actual source code of the webpage. It just contains a link to the remote page. How can I dump the source code of webpage into a local file using CasperJs? getHTML() is also only echoing the contents onto the terminal. How to save the contents to a file?
First import file system library
var fs = require('fs');
Extract html
var html = this.getHTML();
// or
var html = this.getPageContent();
Copy into a file
var f = fs.open('/path/to/your/file', 'w');
f.write(html);
f.close();
do just: fs.write('path/to/file', 'your string', 'w');
in this case you don't need open and close a file

Categories