1

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 Answers1

3
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);
Juho
  • 427
  • 2
  • 12
  • 1
    Thanks A lot!voted up!,just one last question :)Assume that I want to test that this really working what you suggest to do?maybe some Unit test or something else that you can think of... –  Jun 20 '15 at 11:42
  • 1
    Unit testing a short server script like this is a pain, since there are no pure, side-effect free functions. If the script's not an ongoing development, I'd say put a different response to each server, and test with web browser. If you might make something a little more complex out of this, it might be worth while to automate the process. You could either use a proper framework, like Protactor, but there's a learning curve and a setup process involved. If you want to make a simple test that does it's job, you could just make http requests with different Hostname-headers and check responses – Juho Jun 20 '15 at 12:41
  • 1
    can you please provide some steps how to simply test it im very new to this topic and I want somehow to verify that this is working...,Thanks a lot! –  Jun 20 '15 at 15:52
  • 1
    For example: Make 3 servers for each of the we-broxy hostsname switch cases, just like the one you have on the last lines. Make a seperate script that makes 3 requests to the proxy: One with "localhost" as hostname header, one as 127.0.0.1, one with something else. Then just check that for example for "localhost" you get back what's running on port 9001. Headers can be set in the options-field of http request. https://docs.nodejitsu.com/articles/HTTP/clients/how-to-create-a-HTTP-request – Juho Jun 20 '15 at 16:35
  • I mark it as answer ,can you please provide your replay as answer please?what do you mean by make a seperate 3 script ,can you please provide example,Thank you very much for the support! –  Jun 20 '15 at 17:45
  • I meant something like this. That's pretty much the simplest way to test this kind of setup, if you wish to avoid diving into a testing framework. I'm not exactly sure, what you're building, but I think you can modify this to fit your needs. http://pastebin.com/4MZat16Y – Juho Jun 20 '15 at 18:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81084/discussion-between-al-bundy-and-juho). –  Jun 20 '15 at 18:24