Skip to content

Instantly share code, notes, and snippets.

@buschtoens
Created December 1, 2012 11:10
Show Gist options
  • Save buschtoens/4181547 to your computer and use it in GitHub Desktop.
Save buschtoens/4181547 to your computer and use it in GitHub Desktop.
Node.js domain distribution
var http = require("http")
, net = require("net")
, fs = require("fs");
try { fs.mkdirSync("/tmp/http", 0777); } catch(e) {}
fs.chmodSync("/tmp/http", 0777);
http.createServer(function(req, res) {
var options = {
method: req.method
, path: req.url
, headers: req.headers
, socketPath: "/tmp/http/" + req.headers.host
};
var proxy_req = http.request(options, function(proxy_res) {
res.writeHead(proxy_res.statusCode, proxy_res.headers);
proxy_res.pipe(res);
});
proxy_req.on("error", function(error) {
res.write("Something went wrong :(\n\n");
res.write("Domain: " + req.headers.host + "\n\n");
res.end();
});
req.pipe(proxy_req);
}).listen(80);
var http = require("http");
http.createServer(function(req, res) {
res.end("Hey, my name is test.com");
}).listen("/tmp/http/test.com");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment