Port forwarding not working for Nodejs application - node.js

I am running my Nodejs application on port 9000. I want the application to be accessible from a public IP address.
I went to my router config and did:
My Nodejs application is still running on localhost:9000 but when I go to
mypublicipaddress:9000
I get this site cannot be reached.
I also tried the netsh command:
netsh interface portproxy add v4tov4 listenport=9000 connectport=9000 connectaddress=192.168.1.12
This did not throw any error message but this also did not work.

I went to my router config and did: [...]
My Nodejs application is still running on localhost:9000
This implies that you've bound your Node.js server to your loopback interface on your machine and are trying to reach it from your router which is impossible. Try to bind the server to an IP address that is reachable by the router or bind it to all IP addresses on your local machine (ie. 0.0.0.0 or ::).
For example:
const http = require('http');
const server = http.createServer((req, res) => { res.end('It works') });
server.listen(9000, '0.0.0.0');
As for the netsh command, I think you should also set the listenaddress to your machine address on your network (or 0.0.0.0). Also, connectaddress must be set to 127.0.0.1:
netsh interface portproxy add v4tov4 listenport=9000 connectport=9000 connectaddress=127.0.0.1 listenaddress=192.168.1.12

Related

node app is not working on digitalocean server

i have clone a app to my droplet and now it is not working and nor showing any error I am new to programming can anybody help me to deploy it.
I have create droplet install node js,npm and mongodb on it and all are working,
my app.js code is
let port = 80;
mongoose.connect(process.env.dbURI, dbOptions)
.then((conn) => {
app.listen(port,'165.22.223.8');
console.log('connection success port:' +port);
})
.catch((err) => console.log(err));
I suspect (!?) 165.22.223.8 is not the IP address of your DigitalOcean droplet. If that is the correct public IP address of the Droplet then, it's possible that you've not configured the Droplet's firewall to permit traffic on port 80. See debugging below.
NOTE Ports <1024 (i.e. 80) require root. It's possible that this is another issue you're facing. You may wish to try (the commonly used) 8080 instead of 80 while you're developing. If you do, replace occurrences of 80 with 8080 in what follows.
The command app.listen attempts to bind the Express server to that specific IP address on port.
I think you'll be able to avoid specifying the host name entirely and just use:
app.listen(port);
To debug (either scenario), ssh onto the Droplet and try curling the localhost's port 80 (assuming that is indeed the value of port) i.e.:
HOST="localhost"
PORT="80" # Or 8080
curl \
--request GET \
http://${HOST}:${PORT}
When that succeeds, you can then try accessing the server remotely. You'll need to determine the Droplet's public IP address and then:
HOST=[[REPLACE-WITH-DROPLET-IP]]
PORT="80" # Or 8080
curl \
--request GET \
http://${HOST}:${PORT}

How to host a node server [duplicate]

I have a pretty straight-forward question. I made a web game with NodeJS, and I can successfully play it by myself with multiple browser windows open side-by-side; however, I'd like to know if it's possible for other local machines to be able to access and play the game with me too.
I naively tried using this url: my-ip-address:8000 and it won't work.
Your node.js server is running on a port determined at the end of the script usually. Sometimes 3000. but can be anything. The correct way for others to access is as you say...
http://your.network.ip.address:port/
e.g.
http://192.168.0.3:3000
Check you have the correct port - and the IP address on the network - not the internet IP.
Otherwise, maybe the ports are being blocked by your router. Try using 8080 or 80 to get around this - otherwise re-configure your router.
If you are using a router then:
Replace server.listen(yourport, 'localhost'); with server.listen(yourport, 'your ipv4 address');
in my machine it is
server.listen(3000, '192.168.0.3');
Make sure yourport is forwarded to your ipv4 address.
On Windows Firewall, tick all on Node.js:Server-side JavaScript.
I had the same question and solved the problem. In my case, the Windows Firewall (not the router) was blocking the V8 machine I/O on the hosting machine.
Go to windows button
Search "Firewall"
Choose "Allow programs to communicate through Firewall"
Click Change Setup
Tick all of "Evented I/O for V8 Javascript" OR "Node.js: Server-side Javascript"
My guess is that "Evented I/O for V8 Javascript" is the I/O process that node.js communicates to outside world and we need to free it before it can send packets outside of the local computer. After enabling this program to communicate over Windows firewall, I could use any port numbers to listen.
One tip that nobody has mentioned yet is to remember to host the app on the LAN-accessible address 0.0.0.0 instead of the default localhost. Firewalls on Mac and Linux are less strict about this address compared to the default localhost address (127.0.0.1).
For example,
gatsby develop --host 0.0.0.0
yarn start --host 0.0.0.0
npm start --host 0.0.0.0
You can then access the address to connect to by entering ifconfig or ipconfig in the terminal. Then try one of the IP addresses on the left that does not end in .255 or .0
Faced similar issue with my Angular Node Server(v6.10.3) which set up in WIndows 10.
http://localhost:4201 worked fine in localhost. But http://{ipaddress}:4201 not working in other machines in local network.
For this I updated the ng serve like this
//Older ng serve in windows command Prompt
ng serve --host localhost --port 4201
//Updated ng serve
//ng serve --host {ipaddress} --port {portno}
ng serve --host 192.168.1.104 --port 4201
After doing this modification able to access my application in other machines in network bt calling this url
http://192.168.1.104:4201
//http://{ipaddress}:4201
The port is probably blocked by your local firewall or router. Hard to tell without details.
But there is a simple solution for which you don't have to mess with firewall rules, run node as a privileded process to serve on port 80, etc...
Check out Localtunnel. Its a great Ruby script/service, which allows you to make any local port available on the internet within seconds. It's certainly not useful for a production setup, but to try out a game with colleagues, it should work just fine!
const express = require('express');
var app = express();
app.listen(Port Number, "Your IP Address");
// e.g.
app.listen(3000, "192.183.190.3");
You can get your IP Address by typing ipconfig in cmd if your Windows user else you can use ifconfig.
After trying many solution and lot of research I did to the following to make sure my localhost is accessible from other machine in same network. I didn't start my server with IPAddress as parameter to listen method as suggested by others in this question. I did the following to make sure my local node js server is accessible from other machine on same local network. My node server is running in Windows 10 machine.
Open "Windows Defender Firewall with Advanced Security"
Select "Inbound Rules" in the left pane.
In the list of available rules, "Node.js Server-side Javascript" has "Block the connection" radio checked. Change this to "Allow the connection".
Please see the attached screenshot:
After these changes, I am able to access my localhost using http://IPAddress:Port/
Thanks.
And Don't Forget To Change in Index.html Following Code :
<script src="http://192.168.1.4:8000/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
var socket = io.connect('http://192.168.1.4:8000');
Good luck!
This worked for me and I think this is the most basic solution which involves the least setup possible:
With your PC and other device connected to the same network , open cmd from your PC which you plan to set up as a server, and hit ipconfig to get your ip address.
Note this ip address. It should be something like "192.168.1.2" which is the value to the right of IPv4 Address field as shown in below format:
Wireless LAN adapter Wi-Fi:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : ffff::ffff:ffff:ffff:ffad%14
IPv4 Address. . . . . . . . . . . : 192.168.1.2
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Start your node server like this : npm start <IP obtained in step 1:3000> e.g. npm start 192.168.1.2:3000
Open browser of your other device and hit the url: <your_ip:3000> i.e. 192.168.1.2:3000 and you will see your website.
put this codes in your server.js :
app.set('port', (80))
app.listen(app.get('port'), () => {
console.log('Node app is running on port', app.get('port'))
})
after that if you can't access app on network disable firewall like this :
ngrok allows you to expose a port on the internet with custom forwarding refs:
$ npx ngrok http 8000
First, check your ipv4 address. In my case my ipv4 address is 192.168.44.112. If you don't know your ipv4 address, run this command on cmd.
ipconfig
Follow this code...
const express = require('express');
const app = express();
const port = process.env.port || 8000
app.get('/', (req, res) => {
res.send("Hello Network!")
});
app.listen(port, '192.168.77.112', ()=>{
console.log(`Listening port on ${port}`)
});
In Ubuntu you can fix this by allowing a specific port or port range:
sudo ufw allow PORT-NUMBER/tcp
example:
sudo ufw allow 3000/tcp
or a range:
sudo ufw allow 3000:3001/tcp
var http = require('http');
http.createServer(function (req, res) {
}).listen(80, '127.0.0.1');
console.log('Server running at http://127.0.0.1:80/');
By default node will run on every IP address exposed by the host on which it runs. You don't need to do anything special. You already knew the server runs on a particular port. You can prove this, by using that IP address on a browser on that machine:
http://my-ip-address:port
If that didn't work, you might have your IP address wrong.
I had this problem. The solution was to allow node.js through the server's firewall.

DNS lookup from dokku container

I'm running a node app inside a dokku container on an Ubuntu server, which also runs bind9 for DNS. In the node app, I'm running express. On the node app, I'm running a DNS reverse lookup on the client's IP like this (simplified):
const dns = require('dns');
const app = require('express')();
app.get('/myhostname', (req, res) => {
dns.reverse(req.headers['x-forwarded-for'], (err, hostnames) => {
res.json({ hostname: hostnames[0] });
});
});
This works fine locally, but once deployed to the dokku container, fails with an ENOTFOUND error. Presumably this is because the app isn't set to use the Ubuntu server as its DNS server. So I tried this right after require('dns'):
dns.setServers([process.env.DNS_SERVERS])
where DNS_SERVERS is set to either the local LAN ip of the server, or its docker internal IP. Either of those addresses results in a delay and eventual timeout trying to get the address.
How should I go about this?
The solution was this:
Set DNS_SERVERS=172.17.0.1
Allow requests through the firewall on the docker0 interface
Make sure bind is listening on 172.17.0.1 and that it is set to allow requests from the 172.17.0.0/16 range
The second item on the list is what I wasn't considering...

Using express Web-app on local area Network [duplicate]

I have a pretty straight-forward question. I made a web game with NodeJS, and I can successfully play it by myself with multiple browser windows open side-by-side; however, I'd like to know if it's possible for other local machines to be able to access and play the game with me too.
I naively tried using this url: my-ip-address:8000 and it won't work.
Your node.js server is running on a port determined at the end of the script usually. Sometimes 3000. but can be anything. The correct way for others to access is as you say...
http://your.network.ip.address:port/
e.g.
http://192.168.0.3:3000
Check you have the correct port - and the IP address on the network - not the internet IP.
Otherwise, maybe the ports are being blocked by your router. Try using 8080 or 80 to get around this - otherwise re-configure your router.
If you are using a router then:
Replace server.listen(yourport, 'localhost'); with server.listen(yourport, 'your ipv4 address');
in my machine it is
server.listen(3000, '192.168.0.3');
Make sure yourport is forwarded to your ipv4 address.
On Windows Firewall, tick all on Node.js:Server-side JavaScript.
I had the same question and solved the problem. In my case, the Windows Firewall (not the router) was blocking the V8 machine I/O on the hosting machine.
Go to windows button
Search "Firewall"
Choose "Allow programs to communicate through Firewall"
Click Change Setup
Tick all of "Evented I/O for V8 Javascript" OR "Node.js: Server-side Javascript"
My guess is that "Evented I/O for V8 Javascript" is the I/O process that node.js communicates to outside world and we need to free it before it can send packets outside of the local computer. After enabling this program to communicate over Windows firewall, I could use any port numbers to listen.
One tip that nobody has mentioned yet is to remember to host the app on the LAN-accessible address 0.0.0.0 instead of the default localhost. Firewalls on Mac and Linux are less strict about this address compared to the default localhost address (127.0.0.1).
For example,
gatsby develop --host 0.0.0.0
yarn start --host 0.0.0.0
npm start --host 0.0.0.0
You can then access the address to connect to by entering ifconfig or ipconfig in the terminal. Then try one of the IP addresses on the left that does not end in .255 or .0
Faced similar issue with my Angular Node Server(v6.10.3) which set up in WIndows 10.
http://localhost:4201 worked fine in localhost. But http://{ipaddress}:4201 not working in other machines in local network.
For this I updated the ng serve like this
//Older ng serve in windows command Prompt
ng serve --host localhost --port 4201
//Updated ng serve
//ng serve --host {ipaddress} --port {portno}
ng serve --host 192.168.1.104 --port 4201
After doing this modification able to access my application in other machines in network bt calling this url
http://192.168.1.104:4201
//http://{ipaddress}:4201
The port is probably blocked by your local firewall or router. Hard to tell without details.
But there is a simple solution for which you don't have to mess with firewall rules, run node as a privileded process to serve on port 80, etc...
Check out Localtunnel. Its a great Ruby script/service, which allows you to make any local port available on the internet within seconds. It's certainly not useful for a production setup, but to try out a game with colleagues, it should work just fine!
const express = require('express');
var app = express();
app.listen(Port Number, "Your IP Address");
// e.g.
app.listen(3000, "192.183.190.3");
You can get your IP Address by typing ipconfig in cmd if your Windows user else you can use ifconfig.
After trying many solution and lot of research I did to the following to make sure my localhost is accessible from other machine in same network. I didn't start my server with IPAddress as parameter to listen method as suggested by others in this question. I did the following to make sure my local node js server is accessible from other machine on same local network. My node server is running in Windows 10 machine.
Open "Windows Defender Firewall with Advanced Security"
Select "Inbound Rules" in the left pane.
In the list of available rules, "Node.js Server-side Javascript" has "Block the connection" radio checked. Change this to "Allow the connection".
Please see the attached screenshot:
After these changes, I am able to access my localhost using http://IPAddress:Port/
Thanks.
And Don't Forget To Change in Index.html Following Code :
<script src="http://192.168.1.4:8000/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
var socket = io.connect('http://192.168.1.4:8000');
Good luck!
This worked for me and I think this is the most basic solution which involves the least setup possible:
With your PC and other device connected to the same network , open cmd from your PC which you plan to set up as a server, and hit ipconfig to get your ip address.
Note this ip address. It should be something like "192.168.1.2" which is the value to the right of IPv4 Address field as shown in below format:
Wireless LAN adapter Wi-Fi:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : ffff::ffff:ffff:ffff:ffad%14
IPv4 Address. . . . . . . . . . . : 192.168.1.2
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Start your node server like this : npm start <IP obtained in step 1:3000> e.g. npm start 192.168.1.2:3000
Open browser of your other device and hit the url: <your_ip:3000> i.e. 192.168.1.2:3000 and you will see your website.
put this codes in your server.js :
app.set('port', (80))
app.listen(app.get('port'), () => {
console.log('Node app is running on port', app.get('port'))
})
after that if you can't access app on network disable firewall like this :
ngrok allows you to expose a port on the internet with custom forwarding refs:
$ npx ngrok http 8000
First, check your ipv4 address. In my case my ipv4 address is 192.168.44.112. If you don't know your ipv4 address, run this command on cmd.
ipconfig
Follow this code...
const express = require('express');
const app = express();
const port = process.env.port || 8000
app.get('/', (req, res) => {
res.send("Hello Network!")
});
app.listen(port, '192.168.77.112', ()=>{
console.log(`Listening port on ${port}`)
});
In Ubuntu you can fix this by allowing a specific port or port range:
sudo ufw allow PORT-NUMBER/tcp
example:
sudo ufw allow 3000/tcp
or a range:
sudo ufw allow 3000:3001/tcp
var http = require('http');
http.createServer(function (req, res) {
}).listen(80, '127.0.0.1');
console.log('Server running at http://127.0.0.1:80/');
By default node will run on every IP address exposed by the host on which it runs. You don't need to do anything special. You already knew the server runs on a particular port. You can prove this, by using that IP address on a browser on that machine:
http://my-ip-address:port
If that didn't work, you might have your IP address wrong.
I had this problem. The solution was to allow node.js through the server's firewall.

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