4 * Copyright(c) 2010 Sencha Inc.
5 * Copyright(c) 2011 TJ Holowaychuk
10 * Setup vhost for the given `hostname` and `server`.
15 * connect.vhost('foo.com',
16 * connect.createServer(...middleware...)
18 * connect.vhost('bar.com',
19 * connect.createServer(...middleware...)
23 * @param {String} hostname
24 * @param {Server} server
29 module.exports = function vhost(hostname, server){
30 if (!hostname) throw new Error('vhost hostname required');
31 if (!server) throw new Error('vhost server required');
32 var regexp = new RegExp('^' + hostname.replace(/[*]/g, '(.*?)') + '$');
33 if (server.onvhost) server.onvhost(hostname);
34 return function vhost(req, res, next){
35 if (!req.headers.host) return next();
36 var host = req.headers.host.split(':')[0];
37 if (req.subdomains = regexp.exec(host)) {
38 req.subdomains = req.subdomains[0].split('.').slice(0, -1);
39 server.emit("request", req, res, next);