Cant access nodejs server using machine ip address instead of localhost - node.js

I am using windows 10 as OS
Iam trying to access my node.js server from another device browser in the same network using my device ip address in our network--> ipv4 192.168.X.X " its obtaind using ipconfigin cmd" and port "4000", i did the following configration in my code:
module.exports = {
server: app,
start: (port) => {
const PORT = port || process.env.PORT || 4000;
server.listen(PORT,'0.0.0.0', () => { console.log(`Listening on port ${PORT}`); });
},
};
I also opened the inbound ports from windows firewall rules for ports 80, 443, 4000.
FireWall Rules Image
When I tried: http://localhost:4000/ -->it works.
When I tried: http://192.168.X.X:4000/ -->it didn't work.
The weird thing is that when I run the same setup on Linux "ubuntu" it works with the same code above "after opening the ports using: ufw allow 80,443,4000 proto tcp".
For debugging with cmd I tried:
netstat -a -o and I got that node is working in localhost:4000 rather than 0.0.0.0:4000
EX: TCP 127.0.0.1:4000 DESKTOP-T18TEC0:0 LISTENING 11628
How I can make it 0.0.0.0:4000 so I can access node.js server by device-network IP 192:168.X.X?
Ping my ip "192.168.X.X" and its pingable.

Related

AWS public ip with port not working for nodejs

I have installed nodejs in aws ubuntu 18.04 version. Added port 3000 in security Group. but node js not working in my public address with port for eg: http://3.xx.xx.xx:3000.
Note: pm2 running with node js
The solution for this is :-
in your backend inside app.js listen on port 3000 or which ever port you wish to.
Go to amazon console, got to security groups associated with your ec2 instance, and under incoming connection add a custom tcp rule with port 3000 and source should be 0.0.0.0/0 and save it
Run netstat -pan | grep 3000 if you noticed that node.js listen on 127.0.0.1 only not 0.0.0.0 so You can't access node.js from outside because it is listening on localhost IP i.e 127.0.0.1.
You need to configure node.js to listen on 0.0.0.0 so it will be able to accept connections on all the IPs of your machine.
For example:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, "0.0.0.0");
console.log('Server running at http://0.0.0.0:3000/');
last step if you did not already, go to the Security Groups tab in the EC2 console. Right click the security group you setup and click edit inbound rules.
Click Add Rule. This time we are going to use a custom TCP rule on port 3000, open to anywhere.

Nodejs on linux not accessible outside Server

tried everything...
port 8008 seems to be open but no luck.
netstat shows 8008 to be listening
I can do curl localhost:8008 but not from an external machine using the ip address of my server
and yes, i want to host my nodejs on port 8008 (not 8080 - im using 8080 for something else)
netstat output:
tcp 0 0 0.0.0.0:8008 0.0.0.0:* LISTEN
Have you tried changing your port
var port = process.env.PORT || 8008; //server.listen(3000, '0.0.0.0');
app.listen(port, 0.0.0.0, function() {
console.log("Listening on " + port);
});
If you have .env insert value PORT=8008
and run your server

Can't access a node express app from vagrant host machine

I am new to networking and assigning ports and things of that nature. I have been using vagrant for some time and have never had any problems trying to get a test environment up and then accessing it through the host machine browser. The set up for this in my Vagrantfile is this:
# network stuff
config.vm.network "forwarded_port", guest: 8000, host: 8000
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.hostname = "test-box-debian"
Now I am trying to learn a bit about node.js, and every tutorial says I can run npm start and indeed this works fine. I can call wget localhost:3000 (port 3000 being the default in express) and in return get the index.html default page from express.
However when I try and access `192.168.33.10:3000' from the host browser, it doesn't work. I can run netstat and get the following as a result:
sudo netstat -ltpn | grep 3000
tcp6 0 0 :::3000 :::* LISTEN 17238/node
I can see that something doesn't look right but I just don't know enough about ports and networking to know what is wrong and how to fix it.
First, ensure your server is listening to the right IP and that you haven't bound the Express listener elsewhere:
.listen(3000), NOT .listen(3000, '127.0.0.1')
Alternatively, try binding the Express server to your private IP or to the wildcard IP and see if that resolves your connectivity issues:
// Wildcard (All IP's) binding
.listen(3000, '0.0.0.0')
// Specific binding
.listen(3000, '192.168.33.10')
Lastly, port 3000 may not be accessible from the host. If none of the above options in your server code work, try adding the following line to your Vagrantfile:
config.vm.network "forwarded_port", guest: 3000, host: 3000
Make sure you don't have a firewall on your VM blocking the port:
sudo iptables -I INPUT -p tcp --dport 3000 -j ACCEPT
Found the answer over at https://stackoverflow.com/a/28474080/1772120.
If your vagrant setting is like
# network stuff
config.vm.network "forwarded_port", guest: 8000, host: 8000
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.hostname = "test-box-debian"
Then your node app should listen to 192.168.33.10:8000
const http = require('http');
const hostname = '192.168.33.10';
const port = 8000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type','text/plain');
res.end('Hello World\n');
})
server.listen(port, hostname, () => {
console.log('Server running at');
})

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'));
});

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