If you don't want your users to have to tell you what their customised CNAME is, you can do a DNS lookup yourself on the host that you receive.
Something like:
var dns = require('dns');
var domains = dns.resolveCname(req.host);
You can easily add this as your own middleware using connect:
app.use(myCustomDomainLookup());
An implementation in a middleware module might be something like this:
var dns = require('dns');
module.exports = function () {
return function (req, res, next) {
if (!recognisedSubdomain(req.host)) {
var domains = dns.resolveCname(req.host);
var validProfiles = domains.map(main.ifThisProfileExists);
req._profile = validProfiles[0]; // Picking first valid profile
}
next();
}
}
EDIT:
Adding code from comment here for clarity.
To integrate with your subdomain middleware, you'll need to override req.host. This means your middleware will need to be use
d before the subdomain middleware.
You should be able to adapt the above 'if' block as follows:
if (!recognisedSubdomain(req.host)) {
var domains = dns.resolveCname(req.host);
req.host = domains[0];
}
Since documentation for resolveCname indicates a list of domains can be returned, you'll have to decide what you want to do if more than one is returned.