【问题标题】:Multiple subdomains with Restify and nodejs带有 Restify 和 nodejs 的多个子域
【发布时间】:2016-03-27 20:29:13
【问题描述】:

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

我已经成功地为公共静态站点和 API 设置了路由。管理员让我头疼。我需要在不同的子域中公开管理员,例如:admin.mysite.com,而 api 和公共站点在www.mysite.com 上提供(www.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);
    });
});

如何配置 restify 以通过 serveStatic 在同一 nodejs 服务器上的不同子域中提供管理员的 HTML UI?

【问题讨论】:

  • 你发现了吗?这些子域是否都托管在同一台机器上?

标签: node.js configuration subdomain restify


【解决方案1】:

试试这样的:

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

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 2015-01-19
    • 1970-01-01
    相关资源
    最近更新 更多