Skip to content

Instantly share code, notes, and snippets.

@HugoMag
Last active February 29, 2020 03:37

Revisions

  1. HugoMag revised this gist Feb 10, 2014. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion node-http-proxy "Error: read ECONNRESET"
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    var util = require('util'),
    fs = require('fs'),
    http = require('http'),
    httpProxy = require('http-proxy');

  2. HugoMag created this gist Feb 10, 2014.
    47 changes: 47 additions & 0 deletions node-http-proxy "Error: read ECONNRESET"
    Original 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');