]> git.cworth.org Git - obsolete/notmuch-web/blob - node_modules/express/node_modules/connect/lib/middleware/vhost.js
Install the "express" node module via npm
[obsolete/notmuch-web] / node_modules / express / node_modules / connect / lib / middleware / vhost.js
1
2 /*!
3  * Connect - vhost
4  * Copyright(c) 2010 Sencha Inc.
5  * Copyright(c) 2011 TJ Holowaychuk
6  * MIT Licensed
7  */
8
9 /**
10  * Setup vhost for the given `hostname` and `server`.
11  *
12  * Examples:
13  *
14  *     connect(
15  *       connect.vhost('foo.com',
16  *         connect.createServer(...middleware...)
17  *       ),
18  *       connect.vhost('bar.com',
19  *         connect.createServer(...middleware...)
20  *       )
21  *     );
22  *
23  * @param {String} hostname
24  * @param {Server} server
25  * @return {Function}
26  * @api public
27  */
28
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);
40     } else {
41       next();
42     }
43   };
44 };