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: