Last active
August 29, 2015 14:01
-
-
Save valentinkostadinov/da8659e2e77302f22b02 to your computer and use it in GitHub Desktop.
AJAX crawling / JavaScript SEO for MEAN stack web apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Redirect _escaped_fragment_ requests to SEO server for HTML snapshots | |
* | |
* Place in your middleware stack. Watch out for correct placement: | |
* - after any static handler | |
* - before app routes (router) | |
* | |
* app.use(require('./seo-express-filter')(app)) | |
* | |
*/ | |
var URL = require('url') | |
var httpProxy = require('http-proxy') | |
module.exports = function(app) { | |
var conf = app.config.seo | |
var seoServerUrl = URL.format({ | |
protocol: 'http', | |
hostname: conf.host || 'localhost', | |
port: conf.port || 8888 | |
}) | |
var proxy = httpProxy.createProxyServer({ | |
target: seoServerUrl | |
}); | |
return function(req, res, next) { | |
if (!/[&\?]_escaped_fragment_=/.test(req.url) || req.method != 'GET') { | |
return next() | |
} | |
console.log('Proxying request', req.url) | |
proxy.web(req, res, function(err) { | |
console.error('Unable to proxy request', err) | |
// pass through on error - avoid failure on bot request | |
next() | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment