forked from milehigh-world/milehigh-club
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Node.js
137 lines (97 loc) · 3.43 KB
/
Node.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
$ npm install vhost
var vhost = require('vhost')
var connect = require('connect')
var vhost = require('vhost')
var app = connect()
app.use(vhost('*.*.example.com', function handle (req, res, next) {
// for match of "foo.bar.example.com:8080" against "*.*.example.com":
console.dir(req.vhost.host) // => 'foo.bar.example.com:8080'
console.dir(req.vhost.hostname) // => 'foo.bar.example.com'
console.dir(req.vhost.length) // => 2
console.dir(req.vhost[0]) // => 'foo'
console.dir(req.vhost[1]) // => 'bar'
}))
var connect = require('connect')
var serveStatic = require('serve-static')
var vhost = require('vhost')
var mailapp = connect()
// add middlewares to mailapp for mail.example.com
// create app to serve static files on subdomain
var staticapp = connect()
staticapp.use(serveStatic('public'))
// create main app
var app = connect()
// add vhost routing to main app for mail
app.use(vhost('mail.example.com', mailapp))
// route static assets for "assets-*" subdomain to get
// around max host connections limit on browsers
app.use(vhost('assets-*.example.com', staticapp))
// add middlewares and main usage to app
app.listen(3000)
var connect = require('connect')
var serveStatic = require('serve-static')
var vhost = require('vhost')
var mainapp = connect()
// add middlewares to mainapp for the main web site
// create app that will server user content from public/{username}/
var userapp = connect()
userapp.use(function (req, res, next) {
var username = req.vhost[0] // username is the "*"
// pretend request was for /{username}/* for file serving
req.originalUrl = req.url
req.url = '/' + username + req.url
next()
})
userapp.use(serveStatic('public'))
// create main app
var app = connect()
// add vhost routing for main app
app.use(vhost('userpages.local', mainapp))
app.use(vhost('www.userpages.local', mainapp))
// listen on all subdomains for user pages
app.use(vhost('*.userpages.local', userapp))
app.listen(3000)
var connect = require('connect')
var serveStatic = require('serve-static')
var vhost = require('vhost')
var mainapp = connect()
// add middlewares to mainapp for the main web site
// create app that will server user content from public/{username}/
var userapp = connect()
userapp.use(function (req, res, next) {
var username = req.vhost[0] // username is the "*"
// pretend request was for /{username}/* for file serving
req.originalUrl = req.url
req.url = '/' + username + req.url
next()
})
userapp.use(serveStatic('public'))
// create main app
var app = connect()
// add vhost routing for main app
app.use(vhost('userpages.local', mainapp))
app.use(vhost('www.userpages.local', mainapp))
// listen on all subdomains for user pages
app.use(vhost('*.userpages.local', userapp))
app.listen(3000)
var connect = require('connect')
var http = require('http')
var vhost = require('vhost')
// create main app
var app = connect()
app.use(vhost('mail.example.com', function (req, res) {
// handle req + res belonging to mail.example.com
res.setHeader('Content-Type', 'text/plain')
res.end('hello from mail!')
}))
// an external api server in any framework
var httpServer = http.createServer(function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.end('hello from the api!')
})
app.use(vhost('api.example.com', function (req, res) {
// handle req + res belonging to api.example.com
// pass the request to a standard Node.js HTTP server
httpServer.emit('request', req, res)
}))
app.listen(3000