Skip to content

Commit d6102e0

Browse files
committed
init
0 parents  commit d6102e0

File tree

8 files changed

+265
-0
lines changed

8 files changed

+265
-0
lines changed

Diff for: .gitignore

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Compiled source #
2+
###################
3+
*.com
4+
*.class
5+
*.dll
6+
*.exe
7+
*.o
8+
*.so
9+
10+
# Packages #
11+
############
12+
# it's better to unpack these files and commit the raw source
13+
# git has its own built in compression methods
14+
*.7z
15+
*.dmg
16+
*.gz
17+
*.iso
18+
*.jar
19+
*.rar
20+
*.tar
21+
*.zip
22+
23+
# Logs and databases #
24+
######################
25+
*.log
26+
*.sql
27+
*.sqlite
28+
29+
# OS generated files #
30+
######################
31+
.DS_Store*
32+
ehthumbs.db
33+
Icon?
34+
Thumbs.db
35+
36+
# Node.js #
37+
###########
38+
lib-cov
39+
*.seed
40+
*.log
41+
*.csv
42+
*.dat
43+
*.out
44+
*.pid
45+
*.gz
46+
47+
pids
48+
logs
49+
results
50+
51+
node_modules
52+
npm-debug.log
53+
54+
# Git #
55+
#######
56+
*.orig
57+
*.BASE.*
58+
*.BACKUP.*
59+
*.LOCAL.*
60+
*.REMOTE.*
61+
62+
# Components #
63+
##############
64+
65+
/build
66+
/components

Diff for: .npmignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

Diff for: .travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_js:
2+
- "0.10"
3+
- "0.11"
4+
language: node_js

Diff for: Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
test:
2+
@NODE_ENV=test ./node_modules/.bin/mocha \
3+
--reporter spec \
4+
--require should
5+
6+
.PHONY: test

Diff for: README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Virtual Host
2+
3+
Previously `connect.vhost()`.
4+
5+
Usage:
6+
7+
```js
8+
var connect = require('connect');
9+
var vhost = require('vhost');
10+
11+
var app = connect();
12+
13+
app.use(vhost('mail.example.com', function(req, res){}));
14+
app.use(vhost('*.example.com', connect()));
15+
app.use(connect());
16+
```
17+
18+
## License
19+
20+
The MIT License (MIT)
21+
22+
Copyright (c) 2014 Jonathan Ong [email protected]
23+
24+
Permission is hereby granted, free of charge, to any person obtaining a copy
25+
of this software and associated documentation files (the "Software"), to deal
26+
in the Software without restriction, including without limitation the rights
27+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28+
copies of the Software, and to permit persons to whom the Software is
29+
furnished to do so, subject to the following conditions:
30+
31+
The above copyright notice and this permission notice shall be included in
32+
all copies or substantial portions of the Software.
33+
34+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
40+
THE SOFTWARE.

Diff for: index.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
28+
module.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+
};

Diff for: package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "vhost",
3+
"description": "virtual domain hosting",
4+
"version": "1.0.0",
5+
"author": {
6+
"name": "Jonathan Ong",
7+
"email": "[email protected]",
8+
"url": "http://jongleberry.com",
9+
"twitter": "https://twitter.com/jongleberry"
10+
},
11+
"license": "MIT",
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/expressjs/vhost.git"
15+
},
16+
"bugs": {
17+
"mail": "[email protected]",
18+
"url": "https://github.com/expressjs/vhost/issues"
19+
},
20+
"devDependencies": {
21+
"mocha": "^1.17.0",
22+
"should": "^3.0.0",
23+
"supertest": "*",
24+
"connect": "*"
25+
},
26+
"scripts": {
27+
"test": "make test"
28+
}
29+
}

Diff for: test/test.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
var connect = require('connect')
3+
var http = require('http');
4+
var request = require('supertest');
5+
6+
var vhost = require('..');
7+
8+
describe('vhost()', function(){
9+
it('should route by Host', function(done){
10+
var app = connect()
11+
, tobi = connect()
12+
, loki = connect();
13+
14+
app.use(vhost('tobi.com', tobi));
15+
app.use(vhost('loki.com', loki));
16+
17+
tobi.use(function(req, res){ res.end('tobi') });
18+
loki.use(function(req, res){ res.end('loki') });
19+
20+
request(app.listen())
21+
.get('/')
22+
.set('Host', 'tobi.com')
23+
.expect('tobi', done);
24+
})
25+
26+
it('should support http.Servers', function(done){
27+
var app = connect()
28+
, tobi = http.createServer(function(req, res){ res.end('tobi') })
29+
, loki = http.createServer(function(req, res){ res.end('loki') })
30+
31+
app.use(vhost('tobi.com', tobi));
32+
app.use(vhost('loki.com', loki));
33+
34+
request(app.listen())
35+
.get('/')
36+
.set('Host', 'loki.com')
37+
.expect('loki', done);
38+
})
39+
40+
it('should support wildcards', function(done){
41+
var app = connect()
42+
, tobi = http.createServer(function(req, res){ res.end('tobi') })
43+
, loki = http.createServer(function(req, res){ res.end('loki') })
44+
45+
app.use(vhost('*.ferrets.com', loki));
46+
app.use(vhost('tobi.ferrets.com', tobi));
47+
48+
request(app.listen())
49+
.get('/')
50+
.set('Host', 'loki.ferrets.com')
51+
.expect('loki', done);
52+
})
53+
54+
it('should 404 unless matched', function(done){
55+
var app = connect()
56+
, tobi = http.createServer(function(req, res){ res.end('tobi') })
57+
, loki = http.createServer(function(req, res){ res.end('loki') })
58+
59+
app.use(vhost('tobi.com', tobi));
60+
app.use(vhost('loki.com', loki));
61+
62+
request(app.listen())
63+
.get('/')
64+
.set('Host', 'ferrets.com')
65+
.expect(404, done);
66+
})
67+
68+
it('should treat dot as a dot', function(done){
69+
var app = connect()
70+
, tobi = http.createServer(function(req, res){ res.end('tobi') })
71+
72+
app.use(vhost('a.b.com', tobi));
73+
74+
request(app.listen())
75+
.get('/')
76+
.set('Host', 'aXb.com')
77+
.expect(404, done);
78+
})
79+
})

0 commit comments

Comments
 (0)