error while using node proxy

2.4k views Asked by At

Im using the following program and when I run it i got following error I was able to run the app but when i put in the browser localhost:3000 I got this error in the console...

**Error: connect ECONNREFUSED**
    at exports._errnoException (util.js:746:11)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1000:19)

This is the program I've very simple node applicaton with just one file with the following code(this is my server/app/.js

var http = require('http'),
    httpProxy = require('http-proxy'),
    url = require('url');

proxy = httpProxy.createProxyServer({});

http.createServer(function (req, res) {



    switch (hostname) {
        case 'localhost':
            proxy.web(req, res, {target: 'http://localhost:9001'});
            break;

    }
}).listen(3005, function () {
    console.log('original proxy listening on port: ' + 3005);
});


http.createServer(function(req, res) {
    res.end("Request received on 9001");
}).listen(9056);

I want to start new proxy server when User click on some URL

I use this module for that,what Im doing wrong?

https://github.com/nodejitsu/node-http-proxy

another thing...when I use this code I got error ...

process.on('uncaughtException', function (err) {
    console.log(err);
});

this is the error now,any idea?

{ [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect' }
{ [Error: socket hang up] code: 'ECONNRESET' }
1

There are 1 answers

7
Juho On BEST ANSWER
http.createServer(function(req, res) {
    res.end("Request received on 9001");
}).listen(9056);

Your HTTP server is listening on port 9056. The proxy tries to connect to a HTTP server on wrong port and throws an error, when connection cannot be established. To avoid errors like this on the future, put port in a variable:

var PORT = 9001;
http.createServer(function(req, res) {
    res.end("Request received on " + PORT);
}).listen(PORT);