Allow Public IP to connect via WebSockets - node.js

I tried to creating a chat with nodejs ws (einaros),this is my code:
Server:
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 80 });
wss.on('connection', function(ws) {
console.log('connecting count:' + wss.clients.length);
});
Client:
var hostname = location.hostname;
var port = 80;
var url = 'ws://'+hostname+':'+port+'/';
window.WebSocket = window.WebSocket || window.MozWebSocket;
var w = new WebSocket(url);
when I test with my computer or some other computers who connect with my router, it works well. However, other clients who visit via Internet cannot make the connection. Of course, they use the public IP, like 218.xxx.xxx.xxx, not 192.168.xxx.xxx.
I wonder how to solve this problem.
Thank you for you answer. But, it may be not the NAT problem. For example, My public IP is 218.100.50.50,and My private Ip is 192.168.1.1. When a connection comes from outside my network and visits 218.100.50.50:80, the router will redirect it to 192.168.1.1:80. There's a web page which can be visited in this way, but still cannot make the connection to my websocket server. This problem really confuses me much.

Sitting behind router with IPv4 leads to problems for routing actual traffic to right direction (computer behind router).
You need to Forward port 80 in router settings to your computer IP. Additionally I would recommend set in router settings preferred IP for your computer so after restart it is more likely not to change local IP address (otherwise can happen).
After those changes, you can even try to connect using Public IP from your own computer, and if it will work - then it should work from external computers.
Additionally check firewall settings to make sure it does not blocks any external traffic to port 80.

Your problem is called NAT: http://en.wikipedia.org/wiki/Network_address_translation
When a connection comes from outside your network (that is, against your public IP), the router doesn't know who's going to receive that connection. Imagine there are 3 PCs and 2 smartphones connected to your router, which one is going to "answer"? Well, what you have to do is tell the router: "dear router, when my friends try to connect on port 80, redirect them to device 192.168.1.37 and port 80 (for example)".
And if you don't want to reconfigure your router again and again to update 192.168.1.37 (because of DHCP random IP assignation) then tell your router to assign the same IP to your PC always (static ip).
So, configure a "static IP" for your PC, and create a port forwarding rule for port 80 to redirect traffic to your server. You can search "server behind router configuration" for more details, there are thousands of tutorials.

Related

Express.JS server to connect to host on remote network

So I got this small express server running. I can connect it to other devices on my local network e.g. mobile and other PC.
However when connecting over my 4g it does not work. Is there any reason for this? I am sure when I ping other private addresses on remote networks it has worked before, why not now?
Code:
const express = require("express");
const server = express();
const PORT = 3000
server.use(express.static("static"));
server.get("/", (req,res)=>{
res.sendFile(__dirname + "/pages/index.html")
});
server.listen(PORT, "0.0.0.0", (req,res) => {
console.log("Listening on port ", PORT)
});
Any information would be apricated I have some networking experience (still a noob just studying) and this really does interest me.
I assume you are trying to reach the server via your local IP. But you are doing it with 4G (in your phone maybe), which means your request is going over the internet while your local IP is only valid in your network.
Even if you are using your public IP, you would probably have to configure port forwarding on your router for it to know how to handle the incoming traffic for this port.
If you host your express server in your private network , you must have access to the express server by create port forwarding, destination nat, or some kind of publishing private services to public world methods.
If you need more help , i need to know more about your network env , your public ip and ...
Feel free to ask
An have a productive day

Can't Access Node.JS over internet

I'm trying to access a simple node.js/express application over the internet, but I can't for the life of me figure out why it's not working. I can access it using http://localhost:3000 and http://192.168.x.x:3000 but not using my external IP address.
Port 3000 is open on my router (double checked with port online port checker tools), and I've added a rule in the firewall to allow the port (Windows 10).
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, "0.0.0.0")
netstat seems to suggest that port 3000 is allowed through the firewall, right?
C:\WINDOWS\system32>netstat -n -a
Active Connections
Proto Local Address Foreign Address State
TCP 0.0.0.0:3000 0.0.0.0:0 LISTENING
You need to forward that port or make a tunnel to use it over internet . By default the ports are blocked. The problem is not the server is not listening the problem is that it is not discoverable from the outside.
To forward the port, Add the forward rule to your router [More info]
For tunnelling you can use ngrok
By doing that you can connect to the server at http://<your_external_ip>:<forwarded_port>
I finally figured out the issue! It has to do with me testing the connection inside of the LAN on a router that doesn't support hairpinning (see point 2 in this stackoverflow answer). Simply accessing the application on a device outside of my LAN does the trick.

pointing node.js app to my ip address and running locally

So I have two questions about the topic. I have purchased a VPS with digitalocean.com to host my node.js app and they gave me an example hello world app to start.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
var html = "hello world";
res.end(html);
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');
So this code works locally and when I host on the VPS I replace 'localhost' with the IP address of my VPS and it is available on the web when I go to http://IP_Address_of_VPS:8080
My first question: How do I host it live on the web but have it point to my IP address? When I replace 'localhost' with my IP address it does not work when I go to http://my_IP_Address:8080
My second question:
I made a test app following a node.js tutorial and here is the code
var PORT = process.env.PORT || 8080;
var express = require('express');
var app = express();
var http = require('http').Server(app);
app.use(express.static(__dirname + '/public'));
http.listen(PORT, function(){
console.log('listening on port *:'+PORT);
});
Where public is the folder with a simple html page. This app works when I run it locally at http://localhost:8080
Can someone explain how the second app runs locally when I did not specify 'localhost' in the http.listen() function? Also how do I point the second app to run from my IP address if the process is any different from the first?
First question
I hope I understood your question right, here is an attempt at an answer :)
Depends a bit on what you mean with 'my ip address'. If it is your public ip adress, you might have to set up port forwarding. Most routers will deny traffic from the outside network (which you are doing when connecting to your own public ip address) to the internal network by default.
If you mean a private ip address (local to the local network) you need to you use your public ip address and set up port fowarding.
When you set up port forwarding to your local machine you might also want to make your private ip address static.
However, I do not recommend using your home computer as a production server. You will need to solve a lot of problems like making your network secure and having low downtime.
Second question
When no hostname is passed to http.listen, the server will accept connections on all adresses. See documentation for more information.
This works, because when you omit arguments in a function call, they will default toundefined. For example, http.listen(8080) might appear as http.listen(8080, undefined, undefined, undefined). Inside the function body these will often be substituted with some default value.
The process of making your app available to the rest of the world should not be any different.

Redirect localhost url in node.js and express

i have a project where users connect to my router, and then enter the address http://192.168.1.50:9091 into the address bar to connect to a page. I would like there to be a way to enter something easier than this long ip address.
here is what's happening so far:
var express = require('express');
var app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server)
, fs = require('fs')
server.listen(9093);
var nRequest = 0;
var nConnexs = 0;
app.get('/', function (req, res) {
res.sendfile(__dirname + '/client_app.html');
});
app.get('/2', function (req, res) { res.sendfile(__dirname +
'/client_app2.html'); });
I am not sure what you mean by:
i have a project where users connect to my router
The Node.js application is sitting on your machine. Until somebody accesses your machine (through IP), the application is never accessed.
As such, you must put something between the user and the application that will direct the user to the IP. For real websites, this is accomplished using DNS. You register a domain name and tell the DNS service the IP of the machine to redirect to.
If you have control over the router (internal network, etc.), you can map an alias to your IP address. It all depends on whether your router runs a DNS server or supports DNSMasq. You will need to check your router manufacturer/model/etc.
Finally, if there are a small number of users that will be accessing your website, you could always have them use the hosts file to map a name to the IP. The location of this file is dependent upon the operating system; just Google: edit hosts file
I assume users are initially connecting to your router's public ip address or a DNS representation of that. If that's the case, you can usually configure your router to do port forwarding such that incoming requests on a particular port are automatically routed to a particular private IP address on your network. In that case, the users would connect to your router, but that request would be automatically forwarded to your internal node server and thus the users would end up directly connected to your node server.
This type of configuration comes with the usual security warnings. Doing this means that your node server must be properly configured against internet attack and your node app must be written with appropriately security precautions - particularly because any compromise to the computer the node server is running on has access to your internal network.
If you show us what users are initially connecting to when they connect to your router, we could offer a more explicit example of how you could configure the router to take advantage of port forwarding.

Hostname not working node.js

My code is as follows:
var http = require('http');
var static = require('node-static');
var file = new static.Server();
http.createServer(function (req, res) {
file.serve(req, res);
}).listen(1337, '127.0.0.1');
When the url is localhost:1337/1.html it works fine. However if I change it to hostname:1337/ where 'hostname' is the hostname of my server, I get unable to establish connection error. In PHP i could easily replace 127.0.0.1 or localhost with hostname. Why isn't the same possible in node.js?
So the problem is when your browser resolves "hostname", DNS gives it your local network IP address like 192.168.0.42, but your code tells node to listen on one and only one IP address: 127.0.0.1, so the connection doesn't work. Replace '127.0.0.1' in your node code with '0.0.0.0' (which means "all IP addresses") and things will work. Be advised that other computers on your local network (like other folks in a coffee shop wifi network) will be able to connect to your application, which is why for development sticking with the loopback IP address (127.0.0.1) and 'localhost' are better choices.

Resources