Forked from HugoMag/node-http-proxy "Error: read ECONNRESET"
Created
September 25, 2017 22:03
-
-
Save NatasaPeic/dbbb82d37bf0a9dcf5c55547c251af5b to your computer and use it in GitHub Desktop.
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 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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment