Last active
September 20, 2017 06:35
-
-
Save bakso/1e73f4794bda7297d6d3c6f2487a22bd to your computer and use it in GitHub Desktop.
super proxy pass
This file contains hidden or 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
var http = require('http'), | |
httpProxy = require('http-proxy'); | |
var url = require('url'); | |
var proxy = httpProxy.createProxyServer({}); | |
proxy.on('proxyReq', function(proxyReq, req, res, options) { | |
proxyReq.setHeader('host', req.query.host); | |
}); | |
var server = http.createServer(function(req, res) { | |
var rawUrl = req.url.replace('/webproxy', ''); | |
var _url = url.parse(rawUrl, true); | |
var query = _url.query; | |
var pathname = _url.pathname; | |
var host = pathname.split(/\/+/)[1]; | |
var realpath = pathname.slice(pathname.indexOf(host) + host.length); | |
_url.path = _url.pathname = realpath; | |
req.query = query; | |
req.query.host = host; | |
req.query.pathname = realpath; | |
console.log(req.url, rawUrl, realpath); | |
req.url = realpath; | |
if (query.host) { | |
proxy.web(req, res, { target: `http://${query.host}` }); | |
} else { | |
res.statusCode = 404; | |
res.end('not speciify server'); | |
} | |
}); | |
console.log("listening on port 4000"); | |
server.listen(4000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment