<commit_before>var childProcess = require('child_process');
var path = require("path");
var _ = require("underscore");
var util = require("util");

/*
 * GET PhantomJS Rendering
 */
exports.index = function(req, res) {
	var childArgs = [path.join(__dirname, '../rasterize.js'), "http://raphaeljs.com/polar-clock.html", "png"];
	var execFile = process.env.PHANTOMJS_PATH || "node_modules/phantomjs/bin/phantomjs";
	var delegate = res;
	var child = childProcess.execFile(execFile, childArgs, function(err, stdout, stderr) {
		var message = JSON.parse(stdout);
		if (message.image) {
			if (message.image) {
				delegate.render('render', { image: message.image });
				child.kill();
			}
		} else {
			console.log("[stdout]",message);
		}
	});

};<commit_msg>made child process into spawned pid and added data and close events<commit_after>
// node modules
var _     = require('underscore'),
    path  = require('path'),
    spawn = require('child_process').spawn,
    util  = require('util');

// render index page
exports.index = function(req, res){

    var uri = req.query.uri || 'http://raphaeljs.com/polar-clock.html';
    var args = [ path.join(__dirname, '../rasterize.js'), uri, 'png' ];
    var exec = process.env.PHANTOMJS_PATH || 'node_modules/phantomjs/bin/phantomjs';
    var phantomjs = spawn(exec, args);
    var json = { image : '', error : '' };
    var result = '';

    // capture result
    phantomjs.stdout.on('data', function(data){
        result += data;
    });

    // capture errors
    phantomjs.stderr.on('data', function(data){
        json.error += data;
    });

    // close pid
    phantomjs.on('close', function(code){

        // test result
        if (result !== ''){
            result = JSON.parse(result);
            if (result.image !== undefined){
                json.image = result.image;
            }
        }

        // render data
        res.render('render', json);

        // exit pid
        phantomjs.kill( code === 0 ? 'SIGHUP' : 'SIGKILL');

    });

};

{'fraction_non_alphanumeric': 0.16807558428642466, 'fraction_numerical': 0.0004972650422675286, 'mean_word_length': 2.7607476635514017, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '758464', 'n_tokens_mistral': 664, 'n_tokens_neox': 619, 'n_words': 140}