Skip to content

Instantly share code, notes, and snippets.

@wondger
Created August 20, 2012 16:05
Show Gist options
  • Save wondger/3405394 to your computer and use it in GitHub Desktop.
Save wondger/3405394 to your computer and use it in GitHub Desktop.
express vhost
var express = require('express');
var s1 = require('./site1/app.js')
,s2 = require('./site2/app.js')
,s3 = require('./site3/app.js')
,s4 = require('./site4/app.js')
,s5 = require('./site5/app.js')
,s6 = require('./site6/app.js')
var site1 = express.vhost('site1.com', s1),
site2 = express.vhost('site2.com', s2),
site3 = express.vhost('site3.com', s3),
site4 = express.vhost('site4.com', s4),
site5 = express.vhost('site5.com', s5);
site6 = express.vhost('site6.com', s6);
var vhosts = express();
/**
* Module dependencies.
*/
var express = require('../..');
// Edit /etc/vhosts
// First app
var one = express();
one.use(express.logger());
one.get('/', function(req, res){
res.send('Hello from app one!')
});
one.get('/:sub', function(req, res){
res.send('requsted ' + req.params.sub);
});
// App two
var two = express();
two.get('/', function(req, res){
res.send('Hello from app two!')
});
// Redirect app
var redirect = express();
redirect.all('*', function(req, res){
console.log(req.subdomains);
res.redirect('http://localhost:3000/' + req.subdomains[0]);
});
// Main app
var app = express();
app.use(express.vhost('*.localhost', redirect))
app.use(express.vhost('localhost', one));
app.use(express.vhost('dev', two));
app.listen(3000);
console.log('Express app started on port 3000');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment