Node / Express与Spooky / Casper js之间的通信 [英] communication between Node/Express and Spooky/Casper js
本文介绍了Node / Express与Spooky / Casper js之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
server.js
var express = (表现);
var site = express.createServer();
var fill = require('./ spooky_fill.js');
site.use(express.static(__ dirname +'/ ..'));
site.get(/,function(req,res){
fs.createReadStream(./ index.html)。pipe(res);});
site.get('/ fill',fill.search);
site.listen(9201);
console.log(服务器侦听http:// localhost:9201);
spooky_fill.js
try {
var Spooky = require('spooky');
} catch(e){
var Spooky = require('../ lib / spooky');
}
exports.search = function(req,res){
var spooky = new Spooky({
child:{
运输:'http'
},
casper:{
logLevel:'debug',
verbose:true
}
},function(err) {
if(err){
e = new Error('无法初始化SpookyJS);
e.details = err;
throw e;
}
var js;
spooky.start('https://www.google.com');
spooky.then(function(){
js = this。评估(function(){
return document;
});
});
res.write(js); //没有任何东西从这里打印出来
res.write(foo); //这是打印出来的
spooky.then(function(){
this.emit('clog' ,'finished');
});
spooky.run();
});
spooky.on('error',function(e,stack){
console.error(e);
if(stack){
console.log(stack);
}
});
/ *
//取消注释此块以查看Casper所说的所有事情。
//有很多。
//他有意见
spooky.on('console',function(line){
console.log(line);
});
* /
spooky.on('clog',function(message){
console.log(message);
});
spooky.on('log',function(log){
if(log.space ==='remote'){
console.log(log.message。替换(/ \-。* /,''));
}
});
};
解决方案
我通过执行以下操作解决了问题:
spooky.then(function(){
this.emit('page.loaded',this.getHTML 'html',true));
});
spooky.on('page.loaded',function(html){
console.log('############# EMIT') ;
res.send(html);
});
I tried to incorporate node/express with spooky js. However, I wasn't able to send back the elements I retrieved using spookyjs. Below is my code. With my code, I only got foo printed out although I intended to get all the DOM elements data with spooky. I was wondering if anyone has the experience how this can be done? Thanks in advance!
server.js
var express = require("express");
var site = express.createServer();
var fill = require('./spooky_fill.js');
site.use(express.static(__dirname + '/..'));
site.get("/", function(req, res) {
fs.createReadStream("./index.html").pipe(res); });
site.get('/fill', fill.search);
site.listen(9201);
console.log("Server listening on http://localhost:9201");
spooky_fill.js
try {
var Spooky = require('spooky');
} catch(e) {
var Spooky = require('../lib/spooky');
}
exports.search = function(req, res) {
var spooky = new Spooky({
child: {
transport: 'http'
},
casper: {
logLevel: 'debug',
verbose: true
}
}, function(err) {
if(err) {
e = new Error('Failed to initialize SpookyJS');
e.details = err;
throw e;
}
var js;
spooky.start('https://www.google.com');
spooky.then(function() {
js = this.evaluate(function() {
return document;
});
});
res.write(js); //nothing is printed out from here
res.write("foo"); //this is printed out
spooky.then(function() {
this.emit('clog', 'finished');
});
spooky.run();
});
spooky.on('error', function(e, stack) {
console.error(e);
if(stack) {
console.log(stack);
}
});
/*
// Uncomment this block to see all of the things Casper has to say.
// There are a lot.
// He has opinions.
spooky.on('console', function (line) {
console.log(line);
});
*/
spooky.on('clog', function(message) {
console.log(message);
});
spooky.on('log', function(log) {
if(log.space === 'remote') {
console.log(log.message.replace(/ \- .*/, ''));
}
});
};
解决方案
I solved the problem by doing something like the following:
spooky.then(function() {
this.emit('page.loaded', this.getHTML('html', true));
});
spooky.on('page.loaded', function (html) {
console.log('###############EMIT');
res.send(html);
});
这篇关于Node / Express与Spooky / Casper js之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文