6
votes

I'm building a service where other companies can host a community at companyname.mydomain.com. I have a Redux reducer called company that holds certain state about that company. I'd like to be able to isomorphically fetch data about that company based on the subdomain requested.

If I have a function getSubdomain:

function getSubdomain(host) {
  const hostWithoutPort = host.split(':')[0];
  return hostWithoutPort.split('.').slice(0, -2).join('.');
}

Then on the client I'd get the subdomain by doing:

const subdomain = getSubdomain(window.location.host);

On the server, since window isn't available and the host is different per request, I'd do:

const subdomain = getSubdomain(request.headers.host);

What's the right strategy to properly isomorphically fetch company data based on this subdomain? I'm thinking maybe I could initialize my store with the subdomain set in the state?

Not sure what the correct tag is here, sorry if the router tags are irrelevant. :/Andrew Rasmussen
what are you using under the hood to process requests? express?lfender6445
@lfender6445: vanilla nodeAndrew Rasmussen