-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (30 loc) · 884 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
/**
* Module dependencies
*/
var http = require('http')
, proxy = require('http-proxy').createProxyServer({})
, mongo = require('mongojs')
var PORT = process.env.PORT;
var server = http.createServer(onrequest);
var db = mongo('spotlets', ['spotlets']);
server.listen(PORT);
function onrequest (req, res) {
var host = req.headers.host;
var parts = host.split(':')[0];
var subs = parts.split('.');
var subdomain = subs[0];
/** @TODO - seperate plylister
if (-1 != subs.indexOf('playlister')) {
return proxy.web(req, res, { target: 'http://127.0.0.1:9000'});
}
*/
db.spotlets.findOne({name: subdomain}, function (err, result) {
if (err || !result) {
if (err) { console.error(err); }
res.statusCode = 404;
res.write("Not found");
res.end();
}
proxy.web(req, res, { target: 'http://127.0.0.1:'+result.port });
});
}