Created
June 12, 2015 08:42
-
-
Save dbushell/3f50c37351bffca47814 to your computer and use it in GitHub Desktop.
Proxy an Express static site and rewrite all content URLs to localhost
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
var http = require('http'), | |
cheerio = require('cheerio'), | |
express = require('express'); | |
module.exports = plugin; | |
function plugin(grunt) | |
{ | |
grunt.registerTask('dbushell_server', 'dbushell.com', function() { | |
var done = this.async(); | |
var staticApp = express(); | |
staticApp.use(express.static('build')); | |
staticApp.listen(9000); | |
var server = http.createServer(function(req, res) { | |
var options = { | |
port : 9000, | |
hostname : 'localhost', | |
path : req.url, | |
method : 'GET' | |
}; | |
var pReq = http.request(options, function(pRes) | |
{ | |
// pRes.setEncoding('utf8'); | |
if (pRes.headers && pRes.headers['content-type'].indexOf('text/html') === 0) { | |
var chunks = []; | |
pRes.on('data', function (chunk) { | |
chunks.push(chunk); | |
}); | |
pRes.on('end', function() { | |
var $ = cheerio.load(new Buffer(chunks.join('')).toString()); | |
// $('a[href]').each(function(i, el) { | |
// var href = $(this).attr('href'); | |
// href = href.replace('http://dbushell-metalsmith.dev', 'http://localhost:8080'); | |
// href = href.replace('http://dbushell.com', 'http://localhost:8080'); | |
// $(this).attr('href', href); | |
// }); | |
var html = $.html(); | |
html = html.replace(/(http:\/\/dbushell-metalsmith.dev)/g, 'http://localhost:8080'); | |
html = html.replace(/(http:\/\/dbushell.com)/g, 'http://localhost:8080'); | |
pRes.headers['content-length'] = html.length; | |
res.writeHead(pRes.statusCode, pRes.headers); | |
res.write(new Buffer(html)); | |
res.end(); | |
}); | |
} else { | |
res.writeHead(pRes.statusCode, pRes.headers); | |
pRes.on('data', function (chunk) { | |
res.write(chunk); | |
}); | |
pRes.on('end', function() { | |
res.end(); | |
}); | |
} | |
}); | |
pReq.end(); | |
}); | |
server.listen(8080); | |
server.on('listening', function() { | |
var address = server.address(); | |
grunt.log.writeln('Serving at http://' + address.address + ':' + address.port); | |
}); | |
server.on('error', function(err) { | |
grunt.fatal(err); | |
}); | |
}); | |
} |
I was experimenting with cheerio
for HTML manipulation but right now a regex does the job.
Obviously not a good solution for live servers.
Probably not a good solution for test servers.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Problem: static site generates with absolute URLs that don't work on
localhost
testing server.Solution 1: rewrite content and templates to use relative URLs.
Solution 2: serve the static site with Express on port
9000
. Serve a proxy on port8080
that intercepts and caches HTML responses in a buffer. Replace absolute URLs fromdbushell.com
tolocalhost
before to send the response.