title: can phantomjs work with node.js?
body: <p>I would like to use phantomjs in my node.js script. there is a <a href="https://github.com/sgentle/phantomjs-node">phantomjs-node</a> library.. but unfortunately the author used this weird coffee script code to explain what he's doing:</p>
  
  <pre><code>phantom = require 'phantom'
  
  phantom.create (ph) -&gt;
    ph.createPage (page) -&gt;
      page.open "http://www.google.com", (status) -&gt;
        console.log "opened google? ", status
        page.evaluate (-&gt; document.title), (result) -&gt;
          console.log 'Page title is ' + result
          ph.exit()
  </code></pre>
  
  <p>now if I were to use phantomjs directly with javascript, it would look something like <a href="https://github.com/ariya/phantomjs/wiki/Quick-Start">this</a>:</p>
  
  <pre><code>var page = require('webpage').create();
  page.open(url, function (status) {
      var title = page.evaluate(function () {
          return document.title;
      });
      console.log('Page title is ' + title);
  });
  </code></pre>
  
  <p>so basically I'm trying to write up the equivalent of the first snippet of code above in normal javascript (by reading the coffee script <a href="http://coffeescript.org/#literals">documentation</a>.. this is what I did:</p>
  
  <pre><code>// file name: phantomTest.js
  
  var phantom = require('phantom');
  
  phantom.create(function(ph) {
      ph.createPage(function(page) {
          page.open('http://www.google.com', function(status) {
              console.log('opened google?', status);
              var title = page.evaluate(function() {
                  return document.title;
              });
              console.log('page title is ' + title);              
          });
      });
      ph.exit();
  });
  </code></pre>
  
  <p>unfortunately it's not working! If I run</p>
  
  <pre><code>node phantomTest.js
  </code></pre>
  
  <p>on the shell, nothing happens.. nothing returns and the process doesn't stop.. any ideas?</p>
  
  <p><strong>update:</strong></p>
  
  <p>I just read this in the phantomjs <a href="http://phantomjs.org/faq.html">faq</a>: </p>
  
  <blockquote>
    <p>Q: Why is PhantomJS not written as Node.js module?</p>
    
    <p>A: The short answer: "No one can serve two masters."</p>
    
    <p>A longer explanation is as follows.</p>
    
    <p>As of now, it is technically very challenging to do so.</p>
    
    <p>Every Node.js module is essentially "a slave" to the core of Node.js,
    i.e. "the master". In its current state, PhantomJS (and its included
    WebKit) needs to have the full control (in a synchronous matter) over
    everything: event loop, network stack, and JavaScript execution.</p>
    
    <p>If the intention is just about using PhantomJS right from a script
    running within Node.js, such a "loose binding" can be achieved by
    launching a PhantomJS process and interact with it.</p>
  </blockquote>
  
  <p>mmm.. could this have something to do with it? but then that whole library wouldn't make sense!</p>
  
  <p><strong>update 2:</strong></p>
  
  <p>I found this code in the <a href="http://net.tutsplus.com/tutorials/javascript-ajax/web-scraping-with-node-js/">web</a> that does the same thing:</p>
  
  <pre><code>var phantom = require('phantom');
  phantom.create(function(ph) {
    return ph.createPage(function(page) {
      return page.open("http://www.google.com", function(status) {
        console.log("opened google? ", status);
        return page.evaluate((function() {
          return document.title;
        }), function(result) {
          console.log('Page title is ' + result);
          return ph.exit();
        });
      });
    });
  });
  </code></pre>
  
  <p>unfortunately that's not working either.. same result!</p>