can't access google cloud compute engine VM through external IP (nodejs) - node.js

For testing purposes, I've created a Google Cloud Compute Engine VM (Debian 9).
I installed nodejs and created a small script. When navigating to the external ip address through my browser, nothing happens.
const express = require('express')()
express.get('/', (req, res) => {
console.log('browser access')
}
express.listen(8000, () => console.log('server is running'))
When navigating to http://[EXTERNAL_IP_ADDRESS]:8000, nothing happens.
I have SSH access through the external IP, it works
I can ping to the external IP address, this works too
When doing 'node app.js' through the terminal (SSH access), I see 'server is running'
I have set a firewall rule to accept all incoming trafic on tcp=8000 (IP range 0.0.0.0/0)
I have the firewall rules default-allow-http (tcp: 80) and default-allow-https (tcp: 443)
I have a static IP address
Is there something I'm missing?
Edit:
When I visit the server (with :8000) through my browser, the page keeps loading. But the message 'browser access' is not send to the console. After let's say 30 seconds, I get an ERR_CONNECTION_TIME_OUT in the browser.
Express version is 4.17.1. I also changed 'express' to 'app'.
When I open a terminal window and do 'curl EXTERNAL_IP_ADDRESS:8000', nothing happens. It seems like it keeps loading.
I changed listen(8000) to listen(8000, '0.0.0.0'). No differences are observed.
Problem solved:
I got it working. I installed ufw on the VM and opened up port 8000. That was the solution for me.

Solution:
Not only did I need to add a firewall rule to my VPC (0.0.0./0 tcp:8000, incoming), I also needed to open the port 8000 on my VM itself. I installed ufw and opened port 8000. It's working now.

Related

Access NodeJS server installed on Linux server

I created my App from this boilerplate
https://github.com/Bikranshu/express-react-boilerplate
Now I uploaded it to a live Linux server and Node server is running.
Screenshot of running server
But I am unable to access it through Browser with IP address of server.
http://ip_address:3000
After waiting long in browser it is showing timeout error.
Please guide me how can I access the node/react app from browser.
Server running at <ipaddress> is a local IP, are you in a different network than the server? If so, you should be typing https://<public ipaddress>:3000
UPDATE
Hosting services usually only forward port 80 (http) or 443 (https.) This means that your port 3000 is not allowed for public access. To fix your problem you need to change the listening port.
Check line 42 on
server/app.js change 'port' to "80" or check package.json and edit npm start to set port to 80

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.

nodejs app wont create server, says ERR_CONNECTION_TIMED_OUT

I'm trying to run a nodejs app on my VPS but for somereason I get a ERR_CONNECTION_TIMED_OUT error.
Here is my code:
const http = require('http');
const server = http.createServer((req, res) => {
res.end();
});
server.on('clientError', (err, socket) => {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
server.listen(8000);
when i cd to my directory and do
node index.js
, it gives me no error, but going to the browser and doing: mysite.com:8000 gives me nothing. The connections times out
EDIT:
when i do curl: enomshop.tk:8000, I get some feedback. Its like i can access from the within the VPS but no access publicly
This is the way you should go about resolving such issues
Check if the process is running
Run ps aux | grep <process> to check if your process is running
Check if the port is being used
Run sudo netstat -plant | grep <port> to check if port is listening or not and is it your process
Check if the service is responding locally
Run telnet 127.0.0.1 <port> and make sure you get a response. If you don't get a response then there are few possible issues you can look into
The process is not started the server listening
The process is not using the correct port
There is a firewall that is blocking the port. There is this for firewall-cmd, and this for ufw
Check if the service is responding externally
You can do telnet <external ip> <port>, if it doesn't work then there few things you should check below
Make sure your server is binding to 0.0.0.0 or <private ip> and not 127.0.0.1
Make sure the VPS server you are hosting on, has your <port> enabled for communication. This is different for each service provider
If you follow these steps, it would solve most common network issues in such cases
I was facing the same issue. To resolve that, just open up / allow the port from network security group (VPS) or Firewall to worldwide access.
And check telnet mysite.com port, it worked for me. Kindly let me know if you still face any issue, I will try to help further.
You will need to open up the 8000 port on your VPS or redirect the traffic from any open port on VPS to 8000

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.

nodejs timed out on all ports when hosting on godaddy server

I've trying to run my nodejs/expressjs application on my godaddy server, but any port I use times out. I've tried using the application on my local device and it works fine. I have a snippet of my connection below.
var app = express();
app.listen(8080, function() {
console.log("Listening on port " + 8080);
});
When I run the program through ssh, I get no errors
node index.js
Listening on port 8080
But when I go to the corresponding location in my browser, I get:
xxx took too long to respond.
ERR_CONNECTION_TIMED_OUT
I'm pretty sure it has to do with running on the godaddy server. If anyone has experience using this service with nodejs, is there a specific port I should be using, or is there any other setup I should do?
Do you have a VPS with GoDaddy right? So I assume you have also root access.
SSH into your GoDaddy server as root and check if the node.js app actually listens on that port:
netstat -tunlp | grep 8080
If you see any result there for the node.js app and that port then the port is open.
By default, there should be a firewall on your server which might block most of the ports and allows only the necessary incoming traffic.
You can check if there is any rule for that port by issuing the command bellow:
iptables -nvL | grep 8080
If any result is returned, then you have to add an iptables rule to allow access to that port. There are multiple methods to do that:
permit full access from your IP access to the server
permit your ip to access port 8080 on the godaddy server
permit outside world to access port 8080 on your server
You could read any iptables guy, it's pretty easy to add/edit/delete firewall rules. Most of the cPanel/WHM servers come with CSF Firewall (which is based on iptables and perl scripts).
In order to allow an ip address to your firewall (if you have CSF Firewall installed) you have to issue the following command:
csf -a ip-address
I hope that helps!

Resources