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?
Asked
Active
Viewed 66 times
-4
-
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 Aug 04 '20 at 01: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 Aug 04 '20 at 01:47
1 Answers
1
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());
});

Helix
- 162
- 3
- 14
-
No problem! I had a similar problem like this before. Glad I could help :) – Helix Aug 04 '20 at 01:53
-
-
Check out this question: https://stackoverflow.com/questions/50708715/is-there-any-way-to-run-node-js-in-a-script-tag It has alot of helpful answers – Helix Aug 04 '20 at 17:59