Skip to content

Instantly share code, notes, and snippets.

@mdobson
Created June 11, 2016 02:10
Show Gist options
  • Save mdobson/dac37cc060cf03828c5a2ec3aa30b210 to your computer and use it in GitHub Desktop.
Save mdobson/dac37cc060cf03828c5a2ec3aa30b210 to your computer and use it in GitHub Desktop.
Messing around with http and zetta
var http = require('http');
var zetta = require('zetta');
var url = require('url');
var zettaApp = zetta();
zettaApp.name('foo');
var basepath = '/base';
var server = http.createServer(function(req, res) {
var replacedPath = req.url.replace(basepath, '');
req.url = replacedPath;
zettaApp.httpServer.server.emit('request', req, res);
});
server.on('upgrade', function(req, socket, headers) {
var replacedPath = req.url.replace(basepath, '');
req.url = replacedPath;
zettaApp.httpServer.server.emit('upgrade', req, socket, headers);
});
server.listen(3000);
zettaApp.use(function(server) {
var argo = server.httpServer.cloud.use(function(handle) {
handle('response', function(env, next) {
var remappedLinks = env.response.body.links.map(function(link) {
var parsedUrl = url.parse(link.href);
parsedUrl.pathname = basepath + parsedUrl.pathname;
link.href = url.format(parsedUrl);
return link;
});
env.response.body.links = remappedLinks;
next(env);
});
});
});
zettaApp._run(function(err) {
if(err) {
console.log(err);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment