How to use node.io for HTML parsing from node.js?

146 Views Asked by At

I am trying to use node.io on node.js to parse a HTML page which i have as a string in a variable.

I am facing trouble with passing the HTML string to my node.io job as an argument.

This is an excerpt of my code at my node file nodeiotest.js:

var nodeIOJob   =    require('./nodeiojobfile.js');
var nodeio = require('node.io');

vat htmlString = 'HTML String Here';

nodeio.start(nodeIOJob.job, function(err, output) {
        console.log(output);
}, true);

The next is an excerpt of my file nodeiojobfile.js:

var nodeio = require('node.io');

var methods = {
   input: ['xxxxxxxxxxxxxxxx'],    // htmlString is suppossed to come here
   run: function (num) {
       console.log(num);
       this.emit('Hello World!');
   }
}

exports.job = new nodeio.Job(methods);

How do I send my htmlString as argument to my job in the other file?

Also, after receiving the file i need to parse it as an HTML and perform some basic CSS selection (ex. getElementById() etc.) and need to calculate offsetHeight of certain HTML elements. The documentation says I can use get() and getHTML() methods to parse a URL's html but what about HTML in a string? How do I parse them?

For testing purposes I am using he following HTML:

<div>
    <p id="p1">
        Testing document
    </p>
</div>

I am trying to select the <p> and then find out its height.

Can anyone help me? Thnx in advance!!

1

There are 1 best solutions below

5
On

I'm not familiar with node.io, but I think you want something like this:

// nodeiotest.js
...
var htmlString = 'HTML String Here';
nodeio.start(nodeIOJob.job(htmlString), function(err, output) {
  console.log(output);
}, true);

// nodeiojobfile.js
var nodeio = require('node.io');

module.exports.job = function(htmlString) { 
  var methods = {
    input: [ htmlString ],
    run  : function (num) { 
      console.log(num);
      this.emit('Hello World!');
    }
  };
  return new nodeio.Job(methods);
};