nodejs request module returning ECONNREFUSED on localhost - node.js

I am trying to make an api call to my localhost but I always get
Error: connect ECONNREFUSED 127.0.0.1:5005 at Object.exports._errnoException (util.js:1022:11)
var url = `http://127.0.0.1:5005/api/entities/myEndpoint?id=121`;
var options = {
url: url
};
request.get(options, function(err, response, body) {
console.log(err, response,body);
});
It does work if I use an IP instead of localhost

Check if your server has bound the port to its external IP only. On Linux, check netstat -tulpen | grep 5005. The output will contain the Server's listener address, e.g. 0.0.0.0:5005 or ::5005 both means that the server listens on all Interfaces for port 5005.
If your result is something like <ip address>:5005 where ip address is different from 127.0.0.1 that means that the server listens on that ip and port combination only and cannot be accessed using localhost, even if you are logged into the server.

Related

Webpage not publicly accessible via port 80

I have the following code written with Node/Koa, which is serving to port 80:
const
koa = require('koa'),
route = require('koa-route'),
network = koa(),
common = require('koa-common'),
PORT = 80;
// enable logger middleware
network.use(common.logger('dev'));
// enable static middleware
network.use(common.static(__dirname + '/public'));
network.use(route.get('/', index));
network.use(route.get('/about', about));
function *index() {
this.body = "<h1>Is this message on my computer, or on yours...?</h1>";
}
function *about() {
this.body = "<h2>How about now...</h2>";
}
var server = network.listen(PORT, function () {
console.log('Listening on port %d', server.address().port);
});
I have reserved an address (168.192.1.91) for the host computer, set up port forwarding to this address on port 80, made an exception in the Windows 10 firewall for port 80 when connected to via any protocol, and tested with You Get Signal:
which confirms that the port is currently open. When I browse to localhost:80 I can see the default page. However, when I type the computer's public IP address into the browser (I'm typing in the one that I've partially obscured, which I believe should be the correct one):
this page fails to load with the following error:
This site can’t be reached
109.[...] took too long to respond.
Try:
Reloading the page
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_TIMED_OUT
and there is no activity in the Koa log (which logs fine when I browse to there via localhost:80). Any ideas what could be blocking the connection?
I have also tried adding the host address as a second parameter like this:
const HOST = "127.0.0.1";
var server = network.listen(PORT, HOST, function () {
console.log('Listening on port %d', server.address().port);
});
This works for the loopback address, but when I specify my public IP I get this error:
C:\Sites\order-server>node --harmony cheese.js
events.js:154
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL [My public IP here]:80
at Object.exports._errnoException (util.js:890:11)
at exports._exceptionWithHostPort (util.js:913:20)
at Server._listen2 (net.js:1221:19)
at listen (net.js:1270:10)
at net.js:1379:9
at _combinedTickCallback (node.js:386:13)
at process._tickCallback (node.js:407:11)
at Function.Module.runMain (module.js:449:11)
at startup (node.js:142:18)
at node.js:939:3
Maybe koa by default only binds to 127.0.0.1 so you can try binding to any host explicitly with .listen(80, '0.0.0.0')
Otherwise if the firewall ant the port forwarding is configured accordingly, the problem could be that your Internet Provider is blocking incoming connections somewhere. You could try useing an Port over 1024, if it only blocks Ports below.

NodeJS DNS Reverse Lookup

I need to get the host name of the client from its IP. I tried to use Node's DNS Reverse module and it works for external IP's but not internal IP's.
const dns = require('dns');
var ip = req.headers['x-forwarded-for'];
dns.reverse(ip, function(err, hostnames) {
if (err) console.log(`Couldn't reverse IP ${ip}:`, err);
else console.log(`IP ${ip} maps to:`, hostnames);
});
I get error ENOTFOUND for my internal IP. But if I use 8.8.8.8, I will get a host name. Am I missing something with my configuration? I have tested on this on the server using CMD to get the host name by ping -a [internal IP] and it works.

nodejs not receiving request on forwarded port

I am trying to get my router to forward requests made to my noip dynamic dns to my local computer where I have a nodjs running, waiting for requests on port 14555.
I have the noip dns set up so that a netcat running locally does receive any calls made to port 14555 (i.e. with a browser):
ncat -l -n -v -v -k -p 14555
Ncat: Version 5.59BETA1 ( http://nmap.org/ncat )
Ncat: Listening on 0.0.0.0:14555
Ncat: Connection from xxx.xxx.xxx.xxx:56409.
GET /socket.io/?EIO=3&transport=polling&t=1430939580269-4 HTTP/1.1
Host: someDomain.ddns.net:14555
Connection: keep-alive
...
NCAT DEBUG: Closing connection.
Ncat: Connection from 84.167.116.141:56411.
However although the call seems to pass everything and arrives at my machine, node doesn't listen to it. It does not receive anything:
HTTP server listening at 0.0.0.0:14555/
My receiving server is configured like this:
var self = this;
// Start the server
this.httpServer = http.createServer(function(req, res){
// Send HTML headers and message
res.writeHead(200,{ 'Content-Type': 'text/html' });
res.end('<h1>Hello Socket Lover!</h1>');
});
//Listen on the port for HTTP requests
this.httpServer.listen(cfg.port, '0.0.0.0');
console.log('HTTP server listening at 0.0.0.0:' + cfg.port + '/');
P.S.: This questions was originally asked on serverfault, but I was asked to move it here.
*edit 1: I did another experiment with an nginx listening on port 80 and forwarding port 80, which had the same result: call arrives on port 80 on local computer but nginx does not receive it.
I found the solution eventually. It was a problem caused by my router.
See my post on super user for the answer.

Can't connect to nodejs server

I run Apache on my server. Going to my address x.x.x.x:port loads the index.html page in /var/www. When I stop the server, I can no longer connect (all good).
Now I start the node server with node server.js (the server.js file below is also located in /var/www).
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(port, 'x.x.x.x');
console.log('Server running at http://x.x.x.x:port/');
This gives the error listen EADDRNOTAVAIL, but I am not running any other node server (there is no other process running at this port).
I have also tried omitting the IP address and just listening thus: listen(port);
This returns no errors, but I cannot connect to the server (Browser says: Firefox can't establish a connection to the server at x.x.x.x:p.)
I have found out the problem. You don't need to specify a host name:
listen(port, 'x.x.x.x')
should just be
listen(port)
otherwise the server will not accept any connection except ones directed at the specified ip.
The port is in use or not available. Try a different port like:
listen(88, 'x.x.x.x');
and see if that connects. Also, make sure that x.x.x.x is actually the ip address of your server. You can listen on all IPs by doing:
listen(88, '0.0.0.0');
or by leaving the host/ip section out entirely. If it does connect on another port, you just need to find what is using the port you want. If it's port 80, use:
sudo netstat -tulpn | grep :80
to get the program using that port.
Sounds like the port is locked up and in use..
The following command will give you a list of node processes running.
ps | grep node
To free up that port, stop the process using the following.
kill <processId>

Publish Node.JS server on the internet

I have a Node.JS server that works fine on localhost. Now I want it accessible from the internet, hosted by my machine. My public IP address (the one that Google tells me I have) does not seem to be "accessible":
https.createServer({
key: privateKey,
cert: certificate
}, server).listen(80, '86.151.23.17');
fails with the following Node.JS error:
Error: listen EADDRNOTAVAIL
at errnoException (net.js:770:11)
at Server._listen2 (net.js:893:19)
at listen (net.js:937:10)
at Server.listen (net.js:994:9)
at dns.js:71:18
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
How can I publish my Node.JS server to my public IP address?
[Note: I do not have another webserver running. Also, I have tried various different ports as suggested here.]
You are most likely behind a router so your public IP is not available anywhere but on the router itself. What you need to do is listening on your private IP (usually somehing in the 192.168.* range) and setup a port forward on your router.
In case you are on Linux you'll also want to use a port >1024 instead of 80 so you don't have to run node as root. When setting up the port forwarding you can simply forward port 80 to whatever port your node server is running on.
const http = require("http");
const hostname = '0.0.0.0';
const port = 80;
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
using 0.0.0.0 will start listing to the public internet I have tested it.
I have experienced the cases that the ISP given router is intercepting default 80 and 443 ports. Even though the ports are opened. So better check server first using a port like 8080 etc.
And also configure port forwarding to a static local address (ipconfig /all assumed your host is windows) then assigned that IP address to your host using host's MAC address.
for a better experience, if you don't have a static IP, use noip.com dynamic domain names to access your server at any time (without knowing IP address).
Your app should listen on other ip address, example
app.listen(3000,'0.0.0.0');
or just
app.listen(3000);
Then you must open port forwarding in your modem. Like this http://www.dlink.com/uk/en/support/faq/routers/wireless-routers/dkt-series/how-do-i-open-up-ports-to-my-computer-port-forwarding-on-this-router
Finally you can see your app at ip address in here https://whatismyipaddress.com/

Resources