Runs a script string in a vm within a child process.
Example usage:
var sandbox = require('node_vm_sandbox');
sandbox.run({
script: 'res = "Hello world!"'
}, function(err, res) {
console.log('Script returned:', res);
});
Usage:
options = {
script: 'res = "Hello world!"'; // String with JavaScript code. Set `res` with your results.
env: path.join(__dirname, 'env.js'); // Optional, path to an environment file.
data: 'abc'; // Optional, will be passed to the environment file.
timeout: 1000; // Optional, time before the script is terminated, default: 1000.
};
callback = function(err, res) {
// res will be 'Hello world!'
};
sandbox.run(options, callback);
To illustrate the intended usage of the environment file, this shows how to inject cheerio
into the environment:
var cheerio = require('cheerio');
exports.init = function(data) {
// 'data' is passed from the options
// This example assumes that data is a HTML page
return {
$: cheerio.load(data)
};
// Now you will be able to use jQuery inside your script!
};