├── .gitignore ├── README.md ├── example └── app.js ├── index.js ├── lib └── vhost.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## koa-vhost 2 | 3 | vhost for koajs 4 | 5 | ### Install 6 | 7 | npm install koa-vhost 8 | 9 | ### Usage 10 | ```js 11 | var koa = require('koa'); 12 | var vhost = require('koa-vhost'); 13 | 14 | var server = koa(); 15 | var server1 = koa(); 16 | var server2 = koa(); 17 | var server3 = koa(); 18 | var server4 = koa(); 19 | var server5 = koa(); 20 | 21 | server1.use(function *(next) { 22 | this.body = 'server1'; 23 | }); 24 | 25 | server2.use(function *(next) { 26 | this.body = 'server2'; 27 | }); 28 | 29 | server3.use(function *(next) { 30 | this.body = 'server3'; 31 | }); 32 | 33 | server4.use(function *(next) { 34 | this.body = 'server4'; 35 | }); 36 | 37 | server5.use(function *(next) { 38 | this.body = 'server5'; 39 | }); 40 | 41 | server.use(vhost('s1.example.com', server1)); 42 | 43 | server.use(vhost(/s2\.example\.com/, server2)); 44 | 45 | server.use(vhost({ 46 | host: 's3.example.com', 47 | app: server3 48 | })); 49 | 50 | server.use(vhost([{ 51 | host: 's4.example.com', 52 | app: server4 53 | }, { 54 | host: /s5\.example\.com/, 55 | app: server5 56 | }])); 57 | 58 | server.use(function * (next) { 59 | this.body = 'default server'; 60 | }); 61 | 62 | server.listen(3000, function() { 63 | console.log('server listening port 3000'); 64 | }); 65 | ``` 66 | Then write to your `/etc/hosts` 67 | ``` 68 | 127.0.0.1 example.com 69 | 127.0.0.1 s1.example.com 70 | 127.0.0.1 s2.example.com 71 | 127.0.0.1 s3.example.com 72 | 127.0.0.1 s4.example.com 73 | 127.0.0.1 s5.example.com 74 | ``` 75 | 76 | Then 77 | 78 | request `s1.example.com:3000` will return `server1`; 79 | 80 | request `s2.example.com:3000` will return `server2`; 81 | 82 | request `s3.example.com:3000` will return `server3`; 83 | 84 | request `s4.example.com:3000` will return `server4`; 85 | 86 | request `s5.example.com:3000` will return `server5`; 87 | 88 | request `example.com:3000` will return `default server`; 89 | 90 | ### License 91 | MIT -------------------------------------------------------------------------------- /example/app.js: -------------------------------------------------------------------------------- 1 | var koa = require('koa'); 2 | var favi = require('koa-favi'); 3 | var vhost = require('..'); 4 | 5 | var server = koa(); 6 | var server1 = koa(); 7 | var server2 = koa(); 8 | var server3 = koa(); 9 | var server4 = koa(); 10 | var server5 = koa(); 11 | 12 | server1.use(function *(next) { 13 | this.body = 'server1'; 14 | yield next; 15 | }); 16 | 17 | server2.use(function *(next) { 18 | this.body = 'server2'; 19 | yield next; 20 | }); 21 | 22 | server3.use(function *(next) { 23 | this.body = 'server3'; 24 | yield next; 25 | }); 26 | 27 | server4.use(function *(next) { 28 | this.body = 'server4'; 29 | yield next; 30 | }); 31 | 32 | server5.use(function *(next) { 33 | this.body = 'server5'; 34 | yield next; 35 | }); 36 | 37 | server.use(favi()); 38 | 39 | server.use(vhost('s1.example.com', server1)); 40 | 41 | server.use(vhost(/s2\.example\.com/, server2)); 42 | 43 | server.use(vhost({ 44 | host: 's3.example.com', 45 | app: server3 46 | })); 47 | 48 | server.use(vhost([{ 49 | host: 's4.example.com', 50 | app: server4 51 | }, { 52 | host: /s5\.example\.com/, 53 | app: server5 54 | }])); 55 | 56 | server.use(function * (next) { 57 | this.body = 'default server'; 58 | yield next; 59 | }); 60 | 61 | server.listen(3000, function() { 62 | console.log('server listening port 3000'); 63 | }); -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./lib/vhost'); -------------------------------------------------------------------------------- /lib/vhost.js: -------------------------------------------------------------------------------- 1 | var util = require('jistype') 2 | , debug = require('debug')('koa-vhost') 3 | , compose = require('koa-compose'); 4 | 5 | module.exports = function(vhosts, app){ 6 | if(util.isObject(vhosts)){ 7 | vhosts = [vhosts]; 8 | }else if((util.isString(vhosts) || util.isRegExp(vhosts)) && !util.isUndefined(app)){ 9 | vhosts = [{ 10 | host: vhosts 11 | , app: app 12 | }]; 13 | } 14 | 15 | if(!util.isArray(vhosts)){ 16 | throw new Error('vhost define error'); 17 | process.exit(1); 18 | } 19 | 20 | // trim host's leading `http://` or `https://` 21 | vhosts.forEach(function(vhost){ 22 | if(util.isString(vhost.host)){ 23 | vhost.host = vhost.host.replace(/^https?:\/\//, ''); 24 | } 25 | }); 26 | 27 | // compose the app's middleware to one middleware 28 | vhosts.forEach(function(vhost){ 29 | vhost.middleware = compose(vhost.app.middleware); 30 | }); 31 | 32 | return function *vhost(next){ 33 | var hostname, vhost, match, length; 34 | 35 | hostname = this.hostname; 36 | length = vhosts.length; 37 | 38 | for(var i = 0; i < length; i++){ 39 | vhost = vhosts[i]; 40 | debug('test host: %s', vhost.host); 41 | if((util.isString(vhost.host) && hostname === vhost.host) || (util.isRegExp(vhost.host) && hostname.match(vhost.host))){ 42 | match = true; 43 | debug('matched host: %s', vhost.host); 44 | break; 45 | } 46 | } 47 | 48 | if(match){ 49 | // matched specific host, so run the server, 50 | // without invoke the next server's middlewares or next middleware 51 | return yield vhost.middleware.call(this/*, next*/); 52 | }else{ 53 | // none server is matched, 54 | // so to next middleware or next server 55 | return yield next; 56 | } 57 | }; 58 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "koa-vhost", 3 | "version": "0.5.0", 4 | "description": "vhost for koa", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [ 10 | "koa", 11 | "vhost" 12 | ], 13 | "author": "Treri", 14 | "license": "MIT", 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/Treri/koa-vhost" 18 | }, 19 | "bugs": { 20 | "url": "https://github.com/Treri/koa-vhost/issues" 21 | }, 22 | "homepage": "https://github.com/Treri/koa-vhost", 23 | "devDependencies": { 24 | "koa": "^0.5.2" 25 | }, 26 | "dependencies": { 27 | "debug": "^1.0.2", 28 | "jistype": "~0.0.4", 29 | "koa-compose": "^2.2.0", 30 | "koa-favi": "^0.1.0" 31 | } 32 | } 33 | --------------------------------------------------------------------------------