raspberryGPIOhtmlserver/node_modules/connect/lib/middleware/vhost.js
node_modules/connect/lib/middleware/vhost.jsRaw
1
2/*!
3 * Connect - vhost
4 * Copyright(c) 2010 Sencha Inc.
5 * Copyright(c) 2011 TJ Holowaychuk
6 * MIT Licensed
7 */
8
9/**
10 * Vhost:
11 *
12 * Setup vhost for the given `hostname` and `server`.
13 *
14 * connect()
15 * .use(connect.vhost('foo.com', fooApp))
16 * .use(connect.vhost('bar.com', barApp))
17 * .use(connect.vhost('*.com', mainApp))
18 *
19 * The `server` may be a Connect server or
20 * a regular Node `http.Server`.
21 *
22 * @param {String} hostname
23 * @param {Server} server
24 * @return {Function}
25 * @api public
26 */
27
28module.exports = function vhost(hostname, server){
29 if (!hostname) throw new Error('vhost hostname required');
30 if (!server) throw new Error('vhost server required');
31 var regexp = new RegExp('^' + hostname.replace(/[^*\w]/g, '\\$&').replace(/[*]/g, '(?:.*?)') + '$', 'i');
32 if (server.onvhost) server.onvhost(hostname);
33 return function vhost(req, res, next){
34 if (!req.headers.host) return next();
35 var host = req.headers.host.split(':')[0];
36 if (!regexp.test(host)) return next();
37 if ('function' == typeof server) return server(req, res, next);
38 server.emit('request', req, res);
39 };
40};