communication between Node/Express and Spooky/Casper js

孤人 提交于 2019-12-06 15:59:21

问题


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(/ \- .*/, ''));
    }
  });
};

回答1:


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);
});


来源:https://stackoverflow.com/questions/23768800/communication-between-node-express-and-spooky-casper-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!