Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including phantom-server with all npm packages installed. Try it out:

var phantomServer = require("phantom-server")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

phantom-server v0.1.0

Allows you to interact with PhantomJS from NodeJS by hosting it as a server

Phantom Server

This project uses the webserver library in PhantomJS to host a simple phantomjs webserver that will run scripts you pass it over http. This allows you to easily run scripts that require the DOM (d3, js enabled web scrapers) from NodeJS.

Example

This is a simple example demonstrating that window will be defined in your evaluate scripts. For a more complex example that uses d3, see examples/d3.

# script.js
/**
 * This is the function that will be run in the page by PhantomJS. It has access
 * anything you would normally have access to in a page.
 */
module.exports = function() {
    return window !== undefined;
};
# index.js
var path = require( 'path' );

var PhantomServer = require( 'phantom-server' ),
    phantomjs_path = require( 'phantomjs-prebuilt' ).path;

var phantom_server = new PhantomServer( phantomjs_path );

phantom_server
    .run( {
        evaluate_scripts: [
            path.join( __dirname, 'script.js' );
        ]
    } )
    .then( function( evaluate_responses ) {
        console.log( evaluate_responses[ 0 ] ); // true
        phantom_server.stop(); // MAKE SURE YOU STOP THE SERVER WHEN YOU ARE DONE
    } );

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free