Skip to content

Instantly share code, notes, and snippets.

@romanenko
Last active December 22, 2015 02:48
Show Gist options
  • Save romanenko/6405504 to your computer and use it in GitHub Desktop.
Save romanenko/6405504 to your computer and use it in GitHub Desktop.
Simple nodejs server, that receives request with _escaped_fragment_ parameter, and renders back client-side gererated html from given host. Useful for single-page apps, with hashbang-style routes, to make them crawlable, and bot-friendly. (Read this great article for details: http://pivotallabs.com/seo-friendly-single-page-apps-in-rails/).
// Bot-mate by Zero.One (http://www.zeroone.st)
// Simple nodejs server, that receives request with _escaped_fragment_ parameter, and renders back client-side gererated html from given host.
// Useful for single-page apps, with hashbang-style routes, to make them crawlable, and bot-friendly.
// (Read this great article for details: http://pivotallabs.com/seo-friendly-single-page-apps-in-rails/)
//
// Requirements: node and phantomjs in your PATH.
// TODO: Exception handling
// TODO: Caching support
// TODO: Add Nginx and Apache config example
//
// Usage:
// $ node bot-mate.js 'http://example.com'
//
var exec = require('child_process').exec,
http = require('http'),
url = require('url');
http.createServer(function (req, res) {
var child, query;
query = url.parse(req.url,true).query;
host_url = process.argv[2] + '/#!' + query['_escaped_fragment_']
console.log('Processing url: ', host_url);
child = exec("phantomjs page-content.js '" + host_url + "'", function (error, stdout, stderr) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(stdout, 'utf-8');
res.end("\n");
console.log('Success!');
});
}).listen(1337, '127.0.0.1');
console.log("Proxy listening at http://127.0.0.1:1337");
// Simple Phantomjs script that takes given url, makes request and renders received document's innerHtml to stdout
//
// Usage:
// $ phantomjs page-content.js 'http://example.com'
//
var page = require('webpage').create(),
system = require("system");
page.open(system.args[1], function (status) {
console.log(page.content);
phantom.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment