Created
February 19, 2019 22:44
-
-
Save karlrwjohnson/06f0e7dbf495e4ec2199bec71c108c9f to your computer and use it in GitHub Desktop.
Quickie proxy that logs requests while forwarding to another service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
const destHost = 'localhost'; | |
const destPort = 4002; | |
http.createServer((req, res) => { | |
console.log(`> ${req.method} ${req.url}`); | |
for (let k in req.headers) { | |
console.log(`> ${k} = ${req.headers[k]}`) | |
} | |
const subReq = http.request({ | |
host: destHost, | |
port: destPort, | |
path: req.url, | |
headers: req.headers, | |
method: req.method, | |
}, subRes => { | |
console.log(`< ${subRes.statusCode}`) | |
for (let k in subRes.headers) { | |
console.log(`< ${k} = ${subRes.headers[k]}`); | |
res.setHeader(k, subRes.headers[k]); | |
} | |
subRes.on('data', chunk => { | |
console.log('<< ' + chunk); | |
res.write(chunk); | |
}); | |
subRes.on('close', () => { | |
console.log('< (response closed)'); | |
res.end(); | |
}); | |
subRes.on('finish', () => { | |
console.log('< (response finished)'); | |
res.end(); | |
}); | |
}); | |
if (req.complete) { | |
console.log('> (request complete with no data)'); | |
subReq.end(); | |
} | |
req.on('data', chunk => { | |
console.log('>> ' + chunk); | |
subReq.write(chunk); | |
if (req.complete) { | |
console.log('> (request complete after data)'); | |
subReq.end(); | |
} | |
}); | |
req.on('close', () => { | |
console.log('> (request closed)'); | |
subReq.end(); | |
}); | |
req.on('aborted', () => { | |
console.log('> (request aborted)'); | |
subReq.end(); | |
}); | |
}).listen(4003); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment