如何在x.myapp.com上动态路由流星应用程序,其中x是动态的?

时间:2015-06-16 08:50:15

标签: meteor routing subdomain

我创建了两个流星应用程序。一个(www.myapp.com)用于显示所有作者姓名的列表,第二个用于显示作者的详细信息,作者名称为子域,如“authorname.myapp.com”。

流星怎么可能?

谢谢,

1 个答案:

答案 0 :(得分:0)

以下方法已在我的应用中成功使用超过1年。 请记住,下面的代码非常简单,应该用作灵感而不是完整的解决方案。

需要meteorhacks:fast-render

我假设每个subdomain的文档都在集合Sites中。

subdomain         Sites collection document:
a.example.com     { domain:'a.example.com }
b.example.com     { domain:'b.example.com }

我们的想法是,在收到HTML时,可以在客户端上执行Sites.findOne()

两者/ collections.js

Sites = new Mongo.Collection('sites')

服务器/ routes.js

FastRender.onAllRoutes( function ( path ) {
    if ( /(css|js|html|map)/.test( path ) ) {
        return;
    }
    // find subdomain:
    var domain = this.headers.host.split( ":" )[0];

    // subscribe to site publication
    // if domain is in db then send data with HTML file
     this.subscribe('site', domain);
} );


Meteor.publish('site', function(domain){
  return Sites.find( { domain : domain } );
})

的客户机/ start.js

   if(Meteor.client){
     // if domain was correct then 
     // user should receive document of collection `sites`
     // together with HTML
     var site = Sites.findOne();


     // using site you can display subdomain specific content
   }

应该对你有所帮助: Meteor/Iron-Router: how to define routes using data from settings.json