[英]communication between Node/Express and Spooky/Casper js
我試圖將節點/表達式與怪異的js合並。 但是,我無法發送回使用spookyjs檢索到的元素。 下面是我的代碼。 使用我的代碼,盡管我打算以詭異的方式獲取所有DOM元素數據,但我只打印了foo。 我想知道是否有人有經驗可以做到這一點? 提前致謝!
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(/ \- .*/, ''));
}
});
};
我通過執行以下操作解決了該問題:
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.