Created
March 22, 2019 15:01
-
-
Save reu/8a61046cf381a193d5b46f2e06fcf440 to your computer and use it in GitHub Desktop.
Simple HTTP proxy
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 { PORT, PROXY_HOST, PROXY_PORT } = process.env; | |
http | |
.createServer((req, res) => { | |
const proxyRequest = http | |
.request({ | |
hostname: PROXY_HOST, | |
port: PROXY_PORT || 80, | |
path: req.url, | |
method: req.method, | |
headers: { | |
...req.headers, | |
host: PROXY_HOST, | |
}, | |
}, proxyRes => { | |
res.writeHead(proxyRes.statusCode, proxyRes.headers); | |
proxyRes.pipe(res, { end: true }) | |
}); | |
req.pipe(proxyRequest, { end: true }); | |
}) | |
.listen(PORT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment