Robert's Creations Gold LogoRobertscreations Inc.
Architecting Multi-Domain Platforms with React SSR & Express.md

When hosting hundreds of distinct domains on a single web application node, traditional routing and virtual hosting paradigms become bottlenecks. This article details the host-header pattern and React Server-Side Rendering (SSR) pipeline configured to render multi-tenant web platforms dynamically.

Host-Header Interception and Context Injection

In our Express framework, every incoming HTTP request passes through a global routing middleware. The application reads the incoming req.headers.host, strips subdomains, and queries the database settings collection. This injects a request-scoped database context containing domain metadata (blog name, site URL, and visual template configuration) directly into the router:

const domain = req.headers.host.split(':')[0].replace(/^www\./g,'');
const settings = await req.db.collection('settings').findOne({ domain: domain });
          

Dynamic React View Engine Resolution

Instead of routing to statically defined template folders, the template engine is resolved at runtime. Express queries the views subfolder mapped to the active settings.template value. Using express-react-views, we feed the layout component with database-backed props, returning rendered HTML directly to the user:

const template = settings.template ? `${settings.template}/index` : `${domain}/index`;
res.render(template, { settings, content: post, sidebar, pages });
          

This design maximizes server resource sharing while enforcing total styling and structural encapsulation across every site in the multi-domain system.