我试图将节点/表达与spooky js合并。但是,我无法使用spookyjs发回我检索到的元素。以下是我的代码。使用我的代码,我只打印出foo,虽然我打算用怪异的方式获取所有DOM元素数据。我想知道是否有人有经验如何做到这一点?提前谢谢!
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(/ \- .*/, ''));
}
});
};
答案 0 :(得分:3)
我通过执行以下操作解决了问题:
spooky.then(function() {
this.emit('page.loaded', this.getHTML('html', true));
});
spooky.on('page.loaded', function (html) {
console.log('###############EMIT');
res.send(html);
});