vhost
Install
$ npm install vhost
API
var vhost =
vhost(hostname, handle)
Create a new middleware function to hand off request to handle
when the incoming
host for the request matches hostname
. The function is called as
handle(req, res, next)
, like a standard middleware.
hostname
can be a string or a RegExp object. When hostname
is a string it can
contain *
to match 1 or more characters in that section of the hostname. When
hostname
is a RegExp, it will be forced to case-insensitive (since hostnames are)
and will be forced to match based on the start and end of the hostname.
When host is matched and the request is sent down to a vhost handler, the req.vhost
property will be populated with an object. This object will have numeric properties
corresponding to each wildcard (or capture group if RegExp object provided) and the
hostname
that was matched.
// for match of "foo.bar.example.com:8080" against "*.*.example.com":reqvhosthost === 'foo.bar.example.com:8080'reqvhosthostname === 'foo.bar.example.com'reqvhostlength === 2reqvhost0 === 'foo'reqvhost1 === 'bar'
Examples
using with connect for static serving
var connect = var serveStatic = var vhost = var mailapp = // add middlewares to mailapp for mail.example.com // create app to serve static files on subdomainvar staticapp = staticapp // create main appvar app = // add vhost routing to main app for mailapp // route static assets for "assets-*" subdomain to get// around max host connections limit on browsersapp // add middlewares and main usage to app app
using with connect for user subdomains
var connect = var serveStatic = var vhost = var mainapp = // add middlewares to mainapp for the main web site // create app that will server user content from public/{username}/var userapp = userappuserapp // create main appvar app = // add vhost routing for main appappapp // listen on all subdomains for user pagesapp app
using with any generic request handler
var connect = var http = var vhost = // create main appvar app = app // an external api server in any frameworkvar httpServer = http app app