-
-
Save Southern/b720b4a237a3e2f579cf to your computer and use it in GitHub Desktop.
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'); | |
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); |
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
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