[英]communication between Node/Express and Spooky/Casper js
I tried to incorporate node/express with spooky js. 我试图将节点/表达式与怪异的js合并。 However, I wasn't able to send back the elements I retrieved using spookyjs.
但是,我无法发送回使用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.
使用我的代码,尽管我打算以诡异的方式获取所有DOM元素数据,但我只打印了foo。 I was wondering if anyone has the experience how this can be done?
我想知道是否有人有经验可以做到这一点? Thanks in advance!
提前致谢!
server.js 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 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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.