Can't connect to my node.js server - node.js

I am having problems with my node.js web server.
My server is listening on port 80 and I can connect to it through localhost:80 but I can't connect through my domain name.
I have a free domain name that is pointing to a dynamic DNS since I have a dynamic IP address. I installed the program needed to update my IP address.
Am I doing something wrong or am I missing something?

I'm guessing this is to do with your server definition.
The default server.listen given in the examples..
server.listen(1337, "127.0.0.1");
Will only listen to connections from localhost. To get it to respond to any request try the following (the host part is optional)
server.listen(1337);

Because of NAT, your computer is not accessible from the internet. Your router is the only device that is accessible, and the only device that has an IP on the internet.
But your router has the ability to forward all data that someone sends to it to another computer in your local network. So if you want to make your computer accessible from the internet, you have to do such a forwarding. You must define it in your routers settings.
Hope this helps.

2 Things to try.
use this, if you haven't
app.listen(3000, "0.0.0.0")
In db.js file, go for this URI:
DB_URI = "postgresql://localhost/database_name";

Related

Can't establish a connection NodeJS

I am a novice at networking, I have a NodeJS server running with server.listen(3000, '10.0.0.7'); and have forwarded port 3000 with the internal IP address of my server being 10.0.0.7. I can connect from another machine on my network by putting in the browser: 10.0.0.7:3000. It is my understanding that my external IP address should connect me to the router which should then forward me to the server I have set up on 3000.
Am I missing something important? Because I am unable to connect via the external IP address.
Also please note that my firewall is disabled.
I am neither a network expert so excuse me if I use any incorrect terms. In my understanding, you are right about the current configuration, however you are missing the last step. By default, your router makes your LAN unreachable from a so called “external ip”. Just for the definition: external ip in this case is every ip that is not in your LAN. Imagine if your router would allow any communication without you explicitly giving permission. Every open port on your LAN would be available for the network that your router connects to. That is definitely not desirable.
Lets look at it with an example, quoted from this article:
your router has an ip of: 5.6.7.8 if you access it from outer network (internet)
your router has an ip of: 192.168.1.1 if you access it from your LAN
your laptop is in your LAN and has an ip address of 192.168.1.10 and you want to expose port 3000 from your laptop to the outer network (internet)
You have 2 options:
You can expose the port via the configuration of the router (e.g.: you map 5.6.7.8:3000 to 192.168.1.10:3000 in the router configuration and now it will be accessible from the outer network on 5.6.7.8:3000. Note that you can choose any free port, ports don’t have to match.). Of course it is only possible if you have the option to configure the router and you are willing to do so. (e.g.: if you are not in a café)
You can use some tunneling tools like Ngrok or OpenSSH, which make a direct tunnel between the external machine and your server. This can also work if you don’t have the ability to change the router configuration.
Hope I was able to help. Good luck.

How do you host a Node server from your computer so everyone can connect to it

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.

Publish NodeJS Server Online

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

how to connect to a server running on computer using computer's IP address?

I am working on IOT project in which I have to change some variables(fans speed, lights, etc). So just as a starter, I created a node.js server and tried to send requests to the server through a local network using local IP as
http://localhost:7000/users=mandar?lights=OFF
or
http://192.168.43.248:7000/users=mandar?lights=OFF
and it works fine.
Now I want to do the same over the internet. So I got Computer's IP address from https://www.google.co.in/search?q=myip and tried to send a request to the following URL:
http://(IP_address):7000/users=mandar?lights=OFF
This time it keeps on loading and finally shows this site can't be loaded.
So what is the right way to connect to the server through the internet?
Thank you.
You have to do port forwarding.
The IP address you get from the google search is the out-facing IP address of your router. However, your router knows your computer by your local IP address (i.e. 192.168.x.y).
You have to configure your router to send packets coming from internet destined to port 7000(or any other port) to your computer's port 7000.
Check your router's documentation on port forwarding. Likely there is a settings page on the web interface of your router that you can do the desired port forwarding. After configuring the router, there are several tools online to test if the port forwarding is actually working. I suggest you use one of those tools to verify the configuration before testing with your project.
You also might want to check if your router has a firewall. You can add an exception to the firewall such that a specific port number is reachable from the internet.

Port Forwarding on website

I'm noob in this field, so please help me understand this:
I have my web application launched on port 8080. I thought that if i forward port 8080 and enter from web browser: myexternalipadress:8080/Index.html it should open my website. please tell me why i'm wrong
Ahh, hosting a website from your house is a fun thing indeed. In my experience, here is how I approach trouble shooting.
1.Making sure your server is configured properly.
Launch the server application
If your client and server are the same machine, make sure you can reach the server on localhost
Access the server from a different computer on your LAN, use your servers lan ip. 192.168.?.?:8080
If you can't get to it from another machine on your LAN, you may have firewall issues on the server
2.Making sure your network is configured properly
This is where port forwarding comes into play. Figure out the LAN ip of the server and log into the router.
Tell your router to forward the port (8080 in this case) to the server LAN ip address.
Test it by telling your friend to access your server on (WAN_IP:8080 in web browser)

Resources