-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplejsonproxy.js
40 lines (33 loc) · 1.41 KB
/
simplejsonproxy.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
var httpProxy = require('http-proxy'), connect = require('connect'), http = require('http'), fs = require('fs'), mkdirp = require('mkdirp')
var bhost="www.bbva.com", record_dir="www2"
connect.createServer( connect.static(record_dir)).listen(8000);
httpProxy.createServer(function (req, res, proxy) { //WORKING PROXY
if(req.url.match(/\.json\?*/)){
proxy.proxyRequest(req, res, { host: 'localhost', port: 8000 })
} else {
proxy.proxyRequest(req, res, { host: bhost, port: 80 })
//try to go to localhost:9001/TLBB/tlbb/esp/index.jsp
}
}).listen(9001);
httpProxy.createServer(function (req, res, proxy) { //RECORDING MODE PROXY
if(req.url.match(/\.json\?*/)){
var options = { host: bhost , port: 80 , path: '/'+req.url}
var request = http.get(options, function(res2){
var imagedata = ''
var dir_array=options.path.split("/")
var filename=dir_array.pop()
res2.setEncoding('binary')
res2.on('data', function(chunk){ imagedata += chunk })
res2.on('end', function(){
mkdirp(record_dir+dir_array.join("/"), function(err) { });
fs.writeFile(record_dir+options.path, imagedata, 'binary', function(err){
if (err){ console.log("error saving file: ", err) }
console.log('File saved:',filename)
})
})
})
proxy.proxyRequest(req, res, { host: bhost, port: 80 })
} else {
proxy.proxyRequest(req, res, { host: bhost, port: 80 })
}
}).listen(9002);