-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
28 lines (22 loc) · 810 Bytes
/
Copy pathserver.js
File metadata and controls
28 lines (22 loc) · 810 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
const http = require('http');
const https = require('https');
var httpProxy = require('http-proxy');
const fs = require('fs');
const options = {
key: fs.readFileSync('ssl/server.key'),
cert: fs.readFileSync('ssl/server.crt')
}
const portMappings = JSON.parse(fs.readFileSync('port-mappings.json'));
const proxy = httpProxy.createProxyServer(options);
proxy.on('error', (e) => {
console.error(e);
});
const forwardLogic = (req, res) => {
let localPort = portMappings[req.headers.host];
console.log(`Forwarding request from:\n${req.headers.host}${req.url} -> http://localhost:${localPort}${req.url}`);
proxy.web(req, res, { target: `http://localhost:${localPort}` });
};
let servers = [
https.createServer(options, forwardLogic).listen(443),
http.createServer(forwardLogic).listen(80)
];