How to route meteor app dynamically on x.myapp.com where x is dynamic?

182 Views Asked by At

I have created two meteor apps. One(www.myapp.com) for displaying list of all authors name and second to show details of author with authors name as subdomain like "authorname.myapp.com".

How it is possible in meteor?

Thanks,

1

There are 1 best solutions below

4
On

Below approach is used successfully in my apps for more than 1 year. Please keep in my mind that below code is very simplified and it should be used as inspiration rather than complete solution.

Requires meteorhacks:fast-render.

I assume that every subdomain has its document in collection Sites.

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

The idea is that execution of Sites.findOne() on the client will be available in the same time when HTML was received.

both/collections.js

Sites = new Mongo.Collection('sites')

server/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 } );
})

client/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
   }

Should be helpful for you: Meteor/Iron-Router: how to define routes using data from settings.json