-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
41 lines (29 loc) · 960 Bytes
/
server.js
File metadata and controls
41 lines (29 loc) · 960 Bytes
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
var http = require('http')
, url = require('url')
, util = require('util')
, ROUTES = require('./params')
, httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({xfwd: true});
function route(hostname){
var target = ROUTES[hostname];
//console.log(hostname, target);
if(!target)throw new Error("Unknow route to " + hostname);
console.log(util.format("=> route to http://%s", target));
return target;
}
var server = http.createServer(function(req, res){
//var pathname = url.parse(req.url).pathname;
console.log(util.format('http://%s/%s', req.headers.host, req.url));
try{
proxy.web(req, res, { target: route(req.headers.host) });
}catch(e){
console.log(util.format("=> Unknown route!"));
res.statusCode = 404;
return res.end();
}
}).listen(80);
server.on('listening', function(){
console.log("Active routes => ");
console.log(ROUTES);
console.log('Ready to proxy U ...');
});