Is it possible to run a nodejs file and use functions FROM that nodejs file from an html file? Or, is there a NPM Package that allows you to manipulate html elements in an html file from a nodejs file?
-
As in RCP or actually calling the client side's node binary? Or maybe you mean webpack? You need to clarify what you're asking.zero298– zero2982020-08-04 01:38:15 +00:00Commented Aug 4, 2020 at 1:38
-
Not sure, I want to be able to use nodejs inside an html file, but I haven't found any answers to it. I do have a nodejs file that loads the html file, although, I want to be able to control nodejs with the html file aswell. Is this possible?Vamp– Vamp2020-08-04 01:47:42 +00:00Commented Aug 4, 2020 at 1:47
Add a comment
|
1 Answer
Actually for your second question there is a package just for that. It's called Cheerio. Here's an example using it:
var cheerio = require('cheerio');
var fs = require('fs');
fs.readFile('path/to/file.html', 'utf8', function(err, data) {
if (err) throw err;
var $ = cheerio.load(data);
$('body').append('<p>Hello World</p>');
console.log($.html());
});
3 Comments
Helix
No problem! I had a similar problem like this before. Glad I could help :)
Vamp
Question: would it be able to add nodejs scripts? so like, a <script> tag that contains nodejs code like fs.readFile()?
Helix
Check out this question: stackoverflow.com/questions/50708715/… It has alot of helpful answers