什么是nodejs express管理多个域?

时间:2014-06-03 12:46:29

标签: node.js express

我有2个域名。例如,example0.org和example1.org。 如何使用express设置nodejs来管理它们? 例如,我想分享

  

/ publicexample0

作为root的文件夹

  

example0.org

  

/ publicexample1

的文件夹

  

example1.org

以root身份

这仅适用于一个域:

var app = express.createServer();
app.get('/', function(req, res) {
    res.send('Hello World');
});
app.listen(3000);

1 个答案:

答案 0 :(得分:3)

我猜你能做的就是利用HTTP host header。它包含:

  

服务器的域名(用于虚拟主机)和TCP端口   服务器正在侦听的号码。端口号可能是   如果端口是请求的服务的标准端口,则省略。   自HTTP / 1.1以来必须提供。

您可以在RFC 2616 - HTTP v1.1

中查看其规范

显然,你可以在Express中阅读你的请求中的标题,并根据其值做出决定。

router.get('/hello', function(req, res){
   var host = req.get('host');
   if(host === 'example0.org'){
    res.send(200,'Welcome from example0.org');
   } else if(host === 'example1.org'){
    res.send(200,'Welcome from example1.org');
   } else {
    res.send(200,'Welcome stranger');
   }
});