简体   繁体   中英

Express, http-proxy-middleware and net::ERR_CONNECTION_REFUSED

I been trying to debug an issue I have with have a Express App thats uses http-proxy-middleware to forward requests to another backend service. There is a third part application that calls my server by making a request that explicitly uses an IP address in the URL it sends. When running locally on my development machine it uses the local IP address of the machine (not localhost or the loopback 127.0.0.1).

I've stripped my server back to the absolute minimum and I'm proxying to a service that just responds with an empty JSON object. So all other factors have been removed. Its just a simple express app and the proxy.

The Server

var express = require('express');
var proxyMiddleware = require('http-proxy-middleware');
var port = 4000;

var app = express();
var options = {
  target: 'http://jsonplaceholder.typicode.com',
  logLevel: 'debug'
}

var apiProxy = proxyMiddleware('/myportfolio', options);
app.use(apiProxy);

app.listen(port, 'localhost', (err) => {
  if (err) {
    console.log(err);
    process.exit(1);
  }

  // So we can see a message whilst it bundles
  console.log(`now running on port: ${port}\n`);
});

My application starts and displays the message

'now running on port: 4000'

I fire up a browser and send the following:

http://localhost:4000/myportfolio

and get

// 20161023082019
// http://localhost:4000/myportfolio
{
}

Which is absolutely fine and the expected answer, so far so good. I can then switch to using the loopback IP address

When I type

http://127.0.0.1:4000/myportfolio

I get

// 20161023082943
// http://127.0.0.1:4000/myportfolio
{
}

however when I use

http://192.168.1.126:4000/myportfolio

I get nothing returned at all and the Chrome network tab shows the following error

GET http://192.168.1.126:4000/myportfolio net::ERR_CONNECTION_REFUSED

and chrome shows the standard cannot connect message.

This site can’t be reached
192.168.1.126 refused to connect.

I've checked the IP address of the local machines and its correct

Its the same as 'Network Utility' is telling me

and checking the address in a terminal gives:

$ ifconfig | grep inet
inet 127.0.0.1 netmask 0xff000000 
inet6 ::1 prefixlen 128 
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
inet6 fe80::4c:3ebe:e51d:819f%en0 prefixlen 64 secured scopeid 0x4 
inet 192.168.1.126 netmask 0xffffff00 broadcast 192.168.1.255
inet6 fe80::18a8:e9ff:fe89:25e7%awdl0 prefixlen 64 scopeid 0x8 
inet6 fe80::694c:e86:8b39:47f7%utun0 prefixlen 64 scopeid 0xa 
inet6 fe80::45eb:e4b2:c242:48b%utun2 prefixlen 64 scopeid 0xc 
inet6 fe80::859:d69c:1aec:7a59%utun1 prefixlen 64 scopeid 0xb 
inet6 fe80::8d7:fd2c:e131:6832%utun3 prefixlen 64 scopeid 0xd 
$ 

and

$ netstat -an | grep 4000
tcp4       0      0  127.0.0.1.4000         *.*                    LISTEN     
$

The third party software also causes this same net::ERR_CONNECTION_REFUSED to occur (as you would expect) but since I've got the job of integrating it with my development I need to find a fix for this problem of calling directly by IP rather than localhost or 127.0.0.1.

OK after about a couple of hours of trawling the internal and finally looking through the issues list for the middleware

the answer was rather simple - I just needed to open out the Express APP by passing in a different host (not the usual 'localhost')

app.listen(port, '0.0.0.0', (err) => {
  if (err) {
    console.log(err);
    process.exit(1);
  }

  // So we can see a message whilst it bundles
  console.log(`now running on port: ${port}\n`);
});

I'll leave this answer here in case its of use to somebody else in the future.

I could have put in my specific IP address but since I'm on DHCP '0.0.0.0' will suffice for now ...

:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM