-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxyserver.js
63 lines (49 loc) · 1.54 KB
/
proxyserver.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
let httpProxy = require("http-proxy");
let http = require("http");
//Logger configuration
var winston = require('winston');
require('winston-daily-rotate-file');
let portNumber = 1980;
var transport = new (winston.transports.DailyRotateFile)({
filename: 'logs/application-%DATE%.log',
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: '40m',
maxFiles: '60d',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json())
});
transport.on('rotate', function(oldFilename, newFilename) {
console.log("On rotation");
});
var logger = winston.createLogger({
transports: [
transport
]
});
let proxy = httpProxy.createProxyServer({});
proxy.on("error", function(err, req, res) {
console.log(err);
res.writeHead(500, {
"Content-Type": "text/plain"
});
logger.error(err.name);
logger.error(err.stack);
logger.error(err.message);
res.write("\nERROR: " + err.name);
res.write("\nERROR: " + err.stack);
res.end("\nERROR MESSAGE: " + err.message);
});
let server = http.createServer(function(request, response) {
let cd = new Date();
let formatted_date = cd.getDate() + "-" +cd.getMonth() + "-" + cd.getFullYear()+" "+cd.getHours()+":"+cd.getMinutes()
console.log(formatted_date+ " request from " +request.headers.host);
let targetUrl = "http://"+request.headers.host;
proxy.web(request, response, { target: targetUrl });
});
server.on("uncaughtException", function(e) {
console.log(e);
});
console.log("listening on port "+portNumber);
server.listen(portNumber);