Skip to content

Instantly share code, notes, and snippets.

@dbushell
Created June 12, 2015 08:42
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dbushell/3f50c37351bffca47814 to your computer and use it in GitHub Desktop.
Proxy an Express static site and rewrite all content URLs to localhost
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);
});
});
}
@dbushell
Copy link
Author

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 port 8080 that intercepts and caches HTML responses in a buffer. Replace absolute URLs from dbushell.com to localhost before to send the response.

@dbushell
Copy link
Author

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