Skip to content

Instantly share code, notes, and snippets.

@Gufran
Last active December 27, 2015 02:09
Show Gist options
  • Save Gufran/7250454 to your computer and use it in GitHub Desktop.
Save Gufran/7250454 to your computer and use it in GitHub Desktop.
[JavaScript] Serving Casperjs over Node Server
var ip_server = '127.0.0.1:8899';
//includes web server modules
var server = require('webserver').create();
//start web server
var service = server.listen(ip_server, function(request, response) {
var links = [];
var casper = require('casper').create();
function getLinks() {
var links = document.querySelectorAll('h3.r a');
return Array.prototype.map.call(links, function(e) {
return e.getAttribute('href')
});
}
casper.start('http://google.com/', function() {
this.fill('form[action="/search"]', { q: 'gooogle' }, true);
});
casper.then(function() {
links = this.evaluate(getLinks);
});
//
casper.run(function() {
response.statusCode = 200;
//sends results as JSON object
response.write(JSON.stringify(links, null, null));
response.close();
});
});
console.log('Server running at http://' + ip_server+'/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment