Webpage not publicly accessible via port 80 - node.js

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.

Related

how to force node.js pm2 to run based on ipv4

currently my hosting uses a new firewall, and according to their plan, they don't allow any http connection based on ipv6 and all connections should use ipv4.
I have a service using node.js and expressJs, and I also use pm2 as a process manager to run my application, my problem is that http requests failed due to using ipv6. How could I force node.js to listen to version 4 IP address on nodeJs app.
The part of my code which I listen to a port:
const app = express();
...MANY MIDDLEWARE app.use();
mongoose.connect(MONGODB_URI, {useNewUrlParser: true, useUnifiedTopology: true})
.then(result => {
app.listen(APP_PORT);
socketServer.listen(SOCKET_PORT, function () {
console.log('server listening to: %j', socketServer.address())
});
})
.catch(err => {
console.log
});
Can I use something like below with express:
var server = http.createServer(app).listen(APP_PORT, APP_IP);
I was curious about it too, but based on a useful link it worked like a charm :
https://github.com/nodejs/node/issues/18041.
I had this block of code :
var server = http.createServer(app);
server.listen(port);
In order to listen on ipv4, I made the following change :
var server = http.createServer(app);
server.listen(port,"127.0.0.1");
Giving the try one more time the port was listening with ipv4, otherwise it is listening on ipv6 :
$ sudo netstat -lntp |grep -i 3000
tcp 0 0 127.0.0.1:3000 0.0.0.0:* LISTEN 20501/node /home/ml

nodejs request module returning ECONNREFUSED on localhost

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.

Use a node app from another device locally

I have a server (10.0.0.12) and my laptop (10.0.0.2) on a local network.
When I run curl http://10.0.0.2:3000 on the server, it works fine. When I run curl http://10.0.0.12:3000 on my laptop, it doesn't work saying site is unavailable.
I am able to ping and ssh into the server from my laptop.
Here is my code to finish the connection:
app.set('port', (3000));
app.listen(app.get('port'), function(){
console.log("Node app running on localhost:" + app.get('port'));
}
I've tried passing in an ip address to the listen() function, but made no difference. I tried passing in 10.0.0.12 (the ip address of the server), 127.0.0.1, and 0.0.0.0 all with the same result.
How can I host my node app on a local network and have everyone who is on the local network be able to access it through the browser?
EDIT: I'm running on CentOS 7.
EDIT2: When I run netstat -lnt, it says this:
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN
Have you tried to just omit the IP address? It should then be available on the IP address of the machine it is running on and the specified port.
As suggested by HA. remove the IP.
As you can see from the documentation:
https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback
If the hostname is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).
P.S. Which is the OS on the server?
Maybe you can try :
app.listen(3000, '0.0.0.0', function(){
console.log("Node app running on 0.0.0.0:3000");
}
A possible issue could be you aren't using the http module?
var http = require('http').Server(app);
http.listen(3000, function () {
console.log('App running on port 3000');
});
A good practice would be set the port like
app.set('port', (3000));
var http = require('http').Server(app);
http.listen(app.get('port'), function () {
console.log('App running on port ' + app.get('port'));
});

socket.io redis store on openshift

I'm trying to set up socket.io on node.js to use redisstore so i can comunicate with pubsub with multiple node on the opeshift platform, but i can't manage to connect to the redis server. I'm using this cartridge.
I tried to connect with
var pub = redis.createClient(process.env.OPENSHIFT_REDIS_DB_PORT,
process.env.OPENSHIFT_REDIS_DB_HOST);
but it doesn't work (and I found out why: createClient() only accept IP addresses) and it fallback to the default port and host, then I ran rhc port-forward:
$ rhc port-forward appname
Checking available ports ... done
Forwarding ports ...
Address already in use - bind(2) while forwarding port 8080. Trying local port 8081
To connect to a service running on OpenShift, use the Local address
Service Local OpenShift
--------------- --------------- ---- ----------------------------------------------
haproxy 127.0.0.1:8080 => 127.5.149.130:8080
haproxy 127.0.0.1:8081 => 127.5.149.131:8080
s_redis_db_host 127.0.0.1:54151 => blabla.appname.rhcloud.com:54151
Press CTRL-C to terminate port forwarding
So I tought I was doing all wrong and I had to set just the port like this:
var pub = redis.createClient(process.env.OPENSHIFT_REDIS_DB_PORT);
but all I get is this
info: socket.io started
events.js:72
throw er; // Unhandled 'error' event
^
Error: Redis connection to 127.0.0.1:54151 failed - connect ECONNREFUSED
at RedisClient.on_error (/var/lib/openshift/532c3790e0b8cd9bb000006b/app-root/runtime/repo/node_modules/socket.io/node_modules/redis/index.js:149:24)
at Socket.<anonymous> (/var/lib/openshift/532c3790e0b8cd9bb000006b/app-root/runtime/repo/node_modules/socket.io/node_modules/redis/index.js:83:14)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:426:14
at process._tickCallback (node.js:415:13)
DEBUG: Program node server.js exited with code 8
I tried to connect via
telnet $OPENSHIFT_REDIS_DB_HOST $OPENSHIFT_REDIS_DB_PORT
And it works fine... Do you have any suggestions? Am I doing it wrong? (I'm still new to redis and socket.io)
(I omitted the rest of the code 'cause I know it works, I have no problem on my local machine, I just can't get the connection...)
Thanks a lot
but it doesn't work (and I found out why: createClient() only accept IP addresses) and it fallback to the default port and host
It does support Host, createClient uses net.createConnection(port, host); that does support hostname.
The following code will help you find the issue:
console.log(process.env);
var pub = redis.createClient(process.env.OPENSHIFT_REDIS_DB_PORT,
process.env.OPENSHIFT_REDIS_DB_HOST, {auth_pass: process.env.OPENSHIFT_REDIS_DB_PASSWORD});
pub.on('error', console.log.bind(console));
pub.on('ready', console.log.bind(console, 'redis ready'));
Does your openshift Redis instance requires AUTH ?
don't know if it is about a recent change on openshift, but i think the problem is in the variables. Although they work for telnet.
you can try this
var redisHost = process.env.OPENSHIFT_REDIS_HOST;
var redisPort = process.env.OPENSHIFT_REDIS_PORT;
var redisPass = process.env.REDIS_PASSWORD;
var client = redis.createClient( redisPort, redisHost );
client.auth( redisPass );

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