Skip to content

Instantly share code, notes, and snippets.

@Southern
Last active December 10, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Southern/b720b4a237a3e2f579cf to your computer and use it in GitHub Desktop.
Save Southern/b720b4a237a3e2f579cf to your computer and use it in GitHub Desktop.
var http = require('http');
var sites = [
{
hosts: [
'something.com',
'www.something.com'
],
directory: 'something_public'
},
{
hosts: [
'somethingelse.com',
'www.somethingelse.com'
],
directory: 'somethingelse_public'
}
];
var server = http.createServer(function(req, res) {
var result = sites.map(function(site) {
// Return false if the host isn't in this site.
if (!req.headers.host ||
!~site.hosts.indexOf(req.headers.host)) return false;
// Return true if it is.
return true;
});
if (!~result.indexOf(true)) {
res.writeHead(404);
return res.end("Couldn't find the requested site.");
}
/*
Handle the rest of the stuff here.
For example:
*/
var site = sites[result.indexOf(true)];
res.writeHead(200);
res.end("Serve directory: " + site.directory + "\n");
});
server.listen(8080);
curl localhost:8080 -H "Host: something.com"
curl localhost:8080 -H "Host: www.something.com"
curl localhost:8080 -H "Host: somethingelse.com"
curl localhost:8080 -H "Host: www.somethingelse.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment