vhost
Install
$ npm install vhost
API
var vhost = require('vhost')
vhost(hostname, server)
Create a new middleware function to hand off request to server
when the incoming
host for the request matches hostname
.
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.
req.vhost.host === 'foo.bar.example.com:8080'
req.vhost.hostname === 'foo.bar.example.com'
req.vhost.length === 2
req.vhost[0] === 'foo'
req.vhost[1] === 'bar'
Examples
using with connect for static serving
var connect = require('connect')
var serveStatic = require('serve-static')
var vhost = require('vhost')
var mailapp = connect()
var staticapp = connect()
staticapp.use(serveStatic('public'))
var app = connect()
app.use(vhost('mail.example.com', mailapp))
app.use(vhost('assets-*.example.com', staticapp))
app.listen(3000)
using with connect for user subdomains
var connect = require('connect')
var serveStatic = require('serve-static')
var vhost = require('vhost')
var mainapp = connect()
var userapp = connect()
userapp.use(function(req, res, next){
var username = req.vhost[0]
req.originalUrl = req.url
req.url = '/' + username + req.url
next()
})
userapp.use(serveStatic('public'))
var app = connect()
app.use(vhost('userpages.local', mainapp))
app.use(vhost('www.userpages.local', mainapp))
app.use(vhost('*.userpages.local', userapp))
app.listen(3000)
License
MIT