-
Notifications
You must be signed in to change notification settings - Fork 15.6k
/
index.js
49 lines (34 loc) · 942 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Module dependencies.
*/
var express = require('../..');
/*
edit /etc/hosts:
127.0.0.1 foo.example.com
127.0.0.1 bar.example.com
127.0.0.1 example.com
*/
// Main server app
var main = express();
main.use(express.logger('dev'));
main.get('/', function(req, res){
res.send('Hello from main app!')
});
main.get('/:sub', function(req, res){
res.send('requested ' + req.params.sub);
});
// Redirect app
var redirect = express();
redirect.all('*', function(req, res){
console.log(req.subdomains);
res.redirect('http://example.com:3000/' + req.subdomains[0]);
});
// Vhost app
var app = express();
app.use(express.vhost('*.example.com', redirect)) // Serves all subdomains via Redirect app
app.use(express.vhost('example.com', main)); // Serves top level domain via Main server app
/* istanbul ignore next */
if (!module.parent) {
app.listen(3000);
console.log('Express started on port 3000');
}