0

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!!

Surender Thakran
  • 3,958
  • 11
  • 47
  • 81
  • Well, after hours of wrecking my brain and my keyboard keys I have decided that it cannot be done, atleast not without making it a very inefficient task.... kindly let me know if anyone finds a light-weight solution for this... – Surender Thakran Nov 21 '13 at 12:22

1 Answers1

0

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);
};
robertklep
  • 198,204
  • 35
  • 394
  • 381
  • thnx robert but a bigger problem for me is to find the height of an element on the server side, i couldn't seem to do it with node.io and jsdom also has a bug regarding it..... could you suggest a solution for this, maybe a server side headless browser like phantomjs or something similar.... – Surender Thakran Nov 21 '13 at 13:12
  • @SurenderThakran yes you're going to have to use a browser-like solution for that (like PhantomJS which you mention). – robertklep Nov 21 '13 at 13:20
  • I was afraid of this. Can you suggest any such browser compatible with `node.js`? PhantomJS does not comes as a node module and had to be used as a child process, I am not sure how to get height of an element if I am running a headless browser in the command-line. – Surender Thakran Nov 21 '13 at 13:30
  • @SurenderThakran perhaps [zombie](https://npmjs.org/package/zombie), but I'm not sure if it implements actual rendering (to get dimensions) – robertklep Nov 21 '13 at 13:38
  • thnx will give it a shot!! – Surender Thakran Nov 21 '13 at 17:13