O'Reilly logo

PhantomJS Cookbook by Rob Friesel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reading a file from PhantomJS

In this recipe, we will expand on the fs module and demonstrate how to read from a file in PhantomJS. We will cover the open method and discuss the stream object that it returns.

Getting ready

To run this recipe, we will need a script that expects to read a file from the filesystem and a target file from which to read.

The script in this recipe is available in the downloadable code repository as recipe09.js under chapter02. If we run the provided example script, we must change to the root directory for the book's sample code.

How to do it…

Consider the following script:

phantom.onError = function(message, trace) { console.error('[Something went wrong!] - ' + message); phantom.exit(1); }; var fs = require('fs'), _name = ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required