-
Notifications
You must be signed in to change notification settings - Fork 47
/
server.js
44 lines (32 loc) · 976 Bytes
/
server.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
// inspired by https://github.com/visionmedia/express/blob/master/examples/vhost/index.js
/*
edit /etc/hosts:
127.0.0.1 foo.example.com
127.0.0.1 bar.example.com
127.0.0.1 example.com
*/
var http = require("http")
var redirecter = require("redirecter")
var Router = require("routes-router")
var vhost = require("./vhost.js")
var main = Router()
main.addRoute("/", function (req, res) {
res.end("Hello from main app!")
})
main.addRoute("/:sub", function (req, res, opts) {
res.end("you requested " + opts.sub)
})
// redirect router
var redirect = Router()
redirect.addRoute("*", function (req, res) {
var subdomains = (req.headers.host || "").split(".")
redirecter(req, res, "http://example.com:3000/" + subdomains[0])
})
var app = Router()
app.addRoute("*", vhost({
"*.example.com": redirect,
"example.com": main
}))
var server = http.createServer(app)
server.listen(3000)
console.log("vhost server started on port 3000")