Skip to content

Instantly share code, notes, and snippets.

@HugoMag
Last active February 29, 2020 03:37
Show Gist options
  • Save HugoMag/8914225 to your computer and use it in GitHub Desktop.
Save HugoMag/8914225 to your computer and use it in GitHub Desktop.
var util = require('util'),
http = require('http'),
httpProxy = require('http-proxy');
function request_handler(proxy, req, res) {
// http(s) requests.
proxy.web(req, res, function (err) {
console.log(err.stack);
res.writeHead(502);
res.end("There was an error. Please try again");
});
// websocket requests.
req.on('upgrade', function (req, socket, head) {
proxy.ws(req, socket, head, function (err) {
console.log(err.stack);
socket.close();
});
});
}
var site_host_http = "http://localhost:8000";
// create the HTTP proxy server.
var http_proxy = httpProxy.createProxyServer({
target: site_host_http, ws:true
});
// listen to error
http_proxy.on('error', function (err, req, res) {
res.writeHead(500, {
'Content-Type': 'text/plain'
});
res.end('Something went wrong. And we are reporting a custom error message.');
});
// launch http server.
var http_server = http.createServer(function(req, res) {
request_handler(http_proxy, req, res);
});
http_server.on('listening',function() {
console.log('ok, http proxy server is running');
});
http_server.listen(80);
util.puts('http proxy server started on port 80');
@tristanguo
Copy link

Hey Man, I have the same issue, did you manage to solve it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment