-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (37 loc) · 1.24 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"use strict"
let http = require('http')
let fs = require('fs')
let request = require('request')
let argv = require('yargs').argv
let logStream = argv.logfile ? fs.createWriteStream(argv.logfile) : process.stdout
//cli >> --logfile log.txt
let localhost = '127.0.0.1'
let scheme = 'http://'
let host = argv.host || localhost
let port = argv.port || (host === localhost ? 8000 : 80)
let destinationUrl = scheme + host + ':' + port
let echoServer = http.createServer((req, res) => {
logStream.write('echoServer\n');
for (let header in req.headers) {
res.setHeader(header, req.headers[header])
}
logStream.write(JSON.stringify(req.headers)+'\n')
req.pipe(res)
})
//listen on port 8000
echoServer.listen(8000)
logStream.write('echoServer listening @ 127.0.0.1:8000\n')
//terminal command >> curl http://127.0.0.1:8000
let proxyServer = http.createServer((req, res) => {
logStream.write('proxyServer\n');
logStream.write(JSON.stringify(req.headers)+'\n')
let url = destinationUrl
if (req.headers['x-destinations-url'])
url = 'http://' + req.headers['x-destinations-url']
let options = {
url: url + req.url
}
req.pipe(request(options)).pipe(res)
})
proxyServer.listen(9000)
logStream.write('proxyServer listening @ 127.0.0.1:9000\n')