具有独立 node.js 服务器的虚拟主机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8503841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Virtual hosting with standalone node.js server
提问by khoomeister
Is there a way to currently do virtual hosting with node.js server (i.e. host multiple domains under one IP) ?
目前有没有办法使用 node.js 服务器进行虚拟托管(即在一个 IP 下托管多个域)?
采纳答案by alessioalex
Sure, you can use bouncyor node-http-proxyspecifically for that.
当然,您可以专门为此使用bouncy或node-http-proxy。
There's also an Express solution. Check out this example.
还有一个 Express 解决方案。看看这个例子。
回答by Nick Sotiros
Web browsers send a the header property 'host' which identifies the domain host they are trying to contact. So the most basic way would be to do:
Web 浏览器发送标头属性“host”,用于标识他们尝试联系的域名主机。所以最基本的方法是:
http = require('http');
server = http.createServer(function(request, response) {
switch(request.headers.host) {
case 'example.com': response.write('<h1>Welcome to example.com</h1>'); break;
case 'not.example.com': response.write('<h1>This is not example.com</h1>'); break;
default:
response.statusCode = 404;
response.write('<p>We do not serve the host: <b>' + request.headers.host + '</b>.</p>');
}
response.end();
});
server.listen(80);
回答by Hola Soy Edu Feliz Navidad
I would recomend express-vhostbecause the others solutions are based on a proxy server, it means that each one of you vhost should open a different port.
我会推荐express-vhost因为其他解决方案基于代理服务器,这意味着你们每个人都应该打开不同的端口。

