EventedMind/iron-router — pull request #800 by DanielDornhardt (2014-08-31 23:06:54+00:00)
Title: Support routing to start below a subdirectory on the server

Hello,

i created this patch to be able to run apps in a subdirectories of a domain.

Eg. if I want to have an app on

mydomain.com/beta ,

it's possible to tell meteor that via a the ROOT_URL environment variable like this:

ROOT_URL="http://mydomain.com/beta" mrt ,

but IronRouter didn't take that into account until now, meaning the URL mydomain.com/beta/hello would match the route "/beta/hello", instead of just "hello".

This means that one would have to either manually prepend the /subdirectory/on/the/server to each route, or invent some other scheme to dynamically create routes when the same app should be available ob different subdirectories on different servers for example.

Meteor provides a runtime variable, __meteor_runtime_config__.ROOT_URL_PATH_PREFIX , which contains the /subdirectory/on/the/server in which the app is running, both on the client and the server.

We therefore can use this variable to prepend it to created URLs in the router and remove it from the URLs when trying to match routes.

This then allows for multiple apps on different subdirectories.

Server side routes seem to work as well with this patch as Meteor takes into account the ROOT_URL_PATH_PREFIX when creating HTTP endpoints.

THE ONLY THING NOT SO NICE about this patch is that it will break existing routes containing the subdirectories in the routes.

So maybe this should be made configurable, or check against manually set ROOT_URL_PATH_PREFIXes manually set by logging a warning to the console? And maybe disable this functionality for these routes?

Please have a look at the patch, see if it makes sense, if I forgot anything obvious and if there's anything I could do to make it more palatable.

Thanks for the great work everybody and best wishes!

PS: I'll submit a patch to CollectionFS too to enable it to work in subdirectories too and crosslink these pull requests so that whoever might be able to use them can use them in conjunction.