Javascript 来自phantomjs的奇怪输出
我有一个脚本如下:Javascript 来自phantomjs的奇怪输出,javascript,node.js,phantomjs,Javascript,Node.js,Phantomjs,我有一个脚本如下: page.open('http://google.com',function(){ var base64 = page.renderBase64('PNG'); system.stdout.writeLine(JSON.stringify({ image: base64 })); page.close(); phantom.exit(0); } 当我使用phantomjs script.js运行这个脚本时,我得到的输出
page.open('http://google.com',function(){
var base64 = page.renderBase64('PNG');
system.stdout.writeLine(JSON.stringify({
image: base64
}));
page.close();
phantom.exit(0);
}
当我使用phantomjs script.js
运行这个脚本时,我得到的输出是{“image”:“content”}
。但是当我使用node index.js
运行相同的脚本时,会产生幻影并运行相同的脚本
未定义:1
{“图像”:“内容”}
未定义的异常输出的原因是什么:1
node.js脚本如下所示:
`
`请编辑您的问题,并包含最小的node.js脚本。如果PhantomJS脚本不产生未定义的输出,唯一合乎逻辑的解释是节点脚本会产生未定义的输出。@Vaviloff我添加了可以重现错误的节点脚本。请看一看。谢谢
var phantomjs = require('phantomjs');
var childProcess = require('child_process');
var spillOver = '';
var phantomProcess = childProcess.spawn(phantomjs.path,['./image.js']);
phantomProcess.stdin.write(JSON.stringify({url:"http://google.com"}) + '\n');
phantomProcess.stdout.on('data', function(data){
var stringData = data.toString();
if(stringData.indexOf('\n')===-1){
spillOver += stringData;
}else{
stringData = spillOver+stringData;
spillOver = '';
}
stringData.split('\n').forEach(d =>{
if(d === '')
return;
var result = JSON.parse(d);
console.log(result);
});
var result = JSON.parse(stringData);
console.log(stringData);
});
phantomProcess.on('exit', function(code){
console.log('exited with code'+code);
});