Last active
February 29, 2020 03:37
Revisions
-
HugoMag revised this gist
Feb 10, 2014 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,4 @@ var util = require('util'), http = require('http'), httpProxy = require('http-proxy'); -
HugoMag created this gist
Feb 10, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,47 @@ var util = require('util'), fs = require('fs'), 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');