I have a Node.js program running on Mac and Windows, both allowing visitors over the internet. My set up is super simple:
var http = require('http');
var s = http.createServer();
s.listen(80, process.argv[2] || '127.0.0.1');
And when I run the server I will use node server.js 0.0.0.0 to trigger process.argv[2] so it doesn't just listen on the request from the local server.
Yet when I move the same server application to Ubuntu, it stops working. For example, if the Ubuntu server has an IP address of 172.18.x.x, it will only response to requests from machines in the same network, having IP address of 172.18.x.x. If a device has an IP address of 172.19.x.x, it doesn't response. It also doesn't response to forwarded internet calls if the forwarded request didn't come from a routing machine that has an IP address of 172.18.x.x!
This likely has nothing to do with Node.js. It is probably your Ubuntu system that is blocking the connection. Specifically, some firewall or security package installed as a dependency by one of your apps, or otherwise.
Related
I've made a node program and I want to host it trough my computer, how can I go about doing so. Currently I'm using "require("net");" function to start the server locally!
var net = require("net");
var server = net.createServer();
server.on("connection", function(socket){
//Stuff happens in here
});
server.listen(process.env.PORT || 6969, function(){
console.log("Server is listening to %j",server.address());
});
To run a Node.js application you have to install Node.js - you can download it HERE. Then in the command line / terminal, you navigate to the location of your code, and run Node.js with your file as an argument:
cd /c/some/location/with/your/file
node file.js
There is now a Node.js process that is listening on your chosen port - so it's hosted locally.
Looking at the Node.js documentation for Server.listen, it looks like the app is already listening for outside connections. The signature (or one of the signatures) is server.listen(PORT, HOST);. And I also see:
If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.
So your app should already be contactable from the outside (although you may have to open the port explicitly depending on firewall rules). So some other computer, if they make a TCP/or whatever request to <the IP address of your computer>:6969 then the server will respond.
I'm actually surprised that the default host is 0.0.0.0 and not the loopback address (localhost or 127.0.0.1 - these are the same address).
Note not my answer look it up here:
https://serverfault.com/questions/271824/node-js-is-not-accessible-from-external-ips-on-ubuntu
You cant access node.js from outsiede because it is listening on localhost (127.0.0.1). You need to configure it to listen on 0.0.0.0, with this it will be able to accept connections on all the IPs of your machine.
server.listen({
host: '0.0.0.0',
port: 6969
});
If i understood your question correctly, you want to host your node app with your computer as a web server. It's pretty easy you can follow this blog.
Basically the steps include:
Getting a Domain name.
Registering your IP with DNS so that your
domain name points to your computer when someone enters that in the
browser.
Running the web server (Node) to handle the requests.
Note: You might face some problems if your ISP(Internet Service
Provider) changes your IP address frequently.
Since you clarified your question is about how can you host your application so other people can use it, it's not really Node-specific.
You have to open the appropriate ports in your system-level firewall and in your router as well (search for your router's model and you'll see it). Web applications always run in port 80 (for HTTP) and port 443 (for HTTPS), both in TCP. Do note that some providers won't allow you to host a server in port 80, so you'll have to try it out to check if that's the case for you.
Your Node application seems to be running in port 6969, so you either set it to use port 80 (which requires admin privileges) or use a reverse proxy to send all traffic to/from port 80 to port 6969. You can do that with nginx, for example.
I have started my Node.js express server running on port 3000 and it works on my local computer the server is running on. Then I have forwarded the port 3000 to the IPv4 adress of my computer in my router but I still cannot connect to the server on other devices.
HAs it something to do with the protocol? Do you know why it doesn't work.
To connect to a server on your own local LAN, you need the following things:
The local IP address of the server. It would typically be something like 192.168.1.x, but in some cases it might be of the form 10.0.0.x.
You need to make sure the computer the server itself is on does not have any local firewall that is blocking incoming http connections. On Windows 10, there is a local firewall that by default blocks incoming http requests so you would have to enable incoming http requests on the desired port in that firewall configuration.
You can then connect to that server from somewhere else on your local network with a URL of the form http://192.168.1.x:3000/ where the 192.168.1.x is the actual local IP address of your server computer and the 3000 is the port the server is running on.
You need to make sure your other devices are actually ON your local network. For example, if it's a phone, you need to make sure it's actually connected to your local network via WiFi and not connected to the visitor's connection that can only reach the internet, not your local network.
You do not need to do any port forwarding in your router. That's something that would be done when connecting to your server from outside your network (like from the internet). I that case, you'd have to connect to your public IP address and have that safely port forwarded to your server. But, since you said you're trying to connect to the server from within your LAN, you don't need any port forwarding on your external firewall.
I'm currently having an issue when trying to port forward my modem to my Node.js server. Sorry if this is a duplicate question, i've spent some time looking at familiar topics.
I'm using the express framework, I've included a small snippet, so you can see what port the node.js server is listening too.
(Quick Question: Do I use server.listen or app.listen in this situation? What's the difference?)
var express = require('express');
var app = express();
var server = require('http').Server(app);
app.listen(4000);
I've also attatched a picture containing:
The node.js server running.
ipconfig (so you can see my local IP address)
and a screenshot of my modems' port forward settings.
When I google "Whats my IP Address" and retrieve my public IP address, then navigate to it I receive an error on my browser stating that the server took to long to respond. I assume the server isn't being port forwarded, however I can't seem to isolate the issue.
Any help would be greatly appreciated?
Your modem/wifi router's public IP address keeps changing frequently every now and then or when your router reboots. This is because of DHCP.
Port forwarding and hacking your wifi router firewall rules to allow public access to your home or office network is an unsafe practice.
That's where simple tools like SocketXP come in handy. SocketXP will provide you a public URL to access your NodeJS server running in your localhost machine like your home/office laptop or server.
Here is a great blog article on how to install and setup SocketXP to access your local NodeJS server from the internet.
If you're wrapping your Express app in a http server, you should listen on it like so server.listen(4000).
Also make sure that your firewall on your computer is not blocking access to that port. A good alternative to the port forwarding + disabling firewall rules is to use a reverse proxy, something like ngrok for example
I was able to successfully access my node server through a local IP address (192.168.XX.XX) on my WiFi network with the following server code:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
server.listen(8080, "0.0.0.0");
I went to 192.168.XX.XX:8080 and accessed the HTML file successfully.
Specifically, I'm using my phone to access the server but if I get off of the network and use cell service, I can't access the server. I looked up my external IP and got 76.XXX.XXX.XXX. When I go to 76.XXX.XXX.XXX:8080 from my phone (without changing any server code), I can't get to the server. I also tried switching to port 80 in the code and it still didn't work.
I have a McAfee firewall going and I tried to open 8080 to be externally accessible. I also turned the firewall completely off but still couldn't connect.
Any ideas?
The seemingly best solution is using Ngrok. It allows you to expose your local server to the internet. You can download and use it here: https://ngrok.com/
So I have the following URL that leads external users to my port-forwarded XAMPP website.
http://ip_to_my_computer:433
However, my node server is on port 466. How do I connect to it? I can't do
var client = io.connect('http://ip_to_my_computer:433:466');
Thanks!
It looks to me like you're trying to initiate a client request from your XAMPP hosted server (like a .php or .js script) to your Node.js instance?
Simply:
var client = io.connect('http://ip_to_my_computer:node_server_port');
And in your case, you claim your Node server is running on port 466.
I'd bet though that your OS will block a port that low - try changing your Node server to listen on a port between 8000 - 9000.