Skip to content
Myr

Add subdomain offset setting

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add a setting "subdomain offset" for the app, which can be used to change the behavior of req.subdomains. This is useful when our "base" domain contains more than two parts, e.g. example.co.uk, and also when we are running locally with domains like xxx.local. The default behavior is still to return all but the last two parts.

authored by Greg Methvin · committed by Greg Methvin · parent 8beb1f2 · Browse files · 2 files+61 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+61 added−6 removed

  • tests:+50 added−0 removed
  • other:+11 added−6 removed

2 files+61 added−6 removed

modified:lib/request.js+11 added-6 removed
399 unmodified lines
400401/**402 * Return subdomains as an array.403 *404 * For example "tobi.ferrets.example.com"405 * would provide `["ferrets", "tobi"]`.404 * Subdomains are the dot-separated parts of the host before the main domain of405 * the app. By default, the domain of the app is assumed to be the last two406 * parts of the host. This can be changed by setting "subdomain offset".407 *408 * For example, if the domain is "tobi.ferrets.example.com":409 * If "subdomain offset" is not set, req.subdomains is `["ferrets", "tobi"]`.410 * If "subdomain offset" is 3, req.subdomains is `["tobi"]`.411 *412 * @return {Array}413 * @api public414 */415416req.__defineGetter__('subdomains', function(){412 return this.get('Host')413 .split('.')414 .slice(0, -2)415 .reverse();417 var offset = this.app.get('subdomain offset');418 if (offset == null) offset = 2;419420 return this.get('Host').split('.').reverse().slice(offset);421});422423/**424 * Short-hand for `url.parse(req.url).pathname`.
68 unmodified lines
modified:test/req.subdomains.js+50 added