具有Restify和nodejs的多个子域

时间:2015-12-21 17:25:35

标签: node.js configuration subdomain restify

我正在使用Restify开发一个nodejs应用程序。应用程序必须为应用程序提供API,基于角度消耗API的静态公共站点和另一个静态但私有的角度站点,管理UI也使用API​​。

我设法为公共静态网站和API设置路由。管理员让我头疼。我需要在不同的子域中公开管理员,例如:admin.mysite.com,而api和公共网站在www.mysite.comwww.mysite.com/api/上的api)上提供。

这是我到目前为止配置它的方式:

// restify route configuration for the public HTML site
server.get(/^\/(?!api)/, restify.serveStatic({
  directory: './client/',
  default: 'index.html'
}));

// restify route configuration for the API, using directory structure for route setup.
routes(function(routes){
    console.log(routes);
    routes.forEach(function(route){
        server[route.method](route.route, route.handler);
    });
});

如何在同一nodejs服务器上的其他子域中通过serveStatic,管理员的HTML UI配置restify?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

app.get('/', function(req, res) {

  var hostname = req.headers.host.split(":")[0];

  if(hostname == "sub1.domain.com")
    res.send("this is sub1 response!");
  else if(hostname == "sub2.domain.com")
    res.send("this is sub2 response!");

});

参考:

http://code4node.com/snippet/http-proxy-with-custom-routing

自:

https://stackoverflow.com/a/18599699/773263