How to run nodejs on my public ip in windows? - node.js

I using windows 10 and want to run nodejs on port 3000 in my public address (just of me and for remote debugging).
So I open the port as say by this answer and I run this code from expressjs
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
Then I get myip address, and tell my friend to open http://256.266.266.266:3000/ in his chrome browser.
But the answer is This site can’t be reached ERR_CONNECTION_TIMED_OUT. why? how do I connect to my nodejs application in my public ip?
I do not have any antivirus or firewall as far as I know.
Telnet telnet 256.266.266.266 3000 say .Could not open connection to the host, on port...
netstat -an | find "3000" say TCP 0.0.0.0:3000 0.0.0.0:0 LISTENING

Related

How to run express server on Shared Hosting?

My shared hosting doesn't allow me customization. But, I anyhow installed nodejs. I also wrote an express server code inside /var/www/test-server directory.
const express = require('express')
const app = express()
const port = 8080
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
• But, I cannot access express server
• I can't use port 80.
• When I use some other ports like 8080, then I can see console output on Terminal as below:
example.work#example:/var/www/test-server$ node index.js
Example app listening on port 8080
Now, my question is —
"Is it possible to access express server with or without port through browser?"
Like: http://example.com/test-server
You must set up NGINX for the express server to be available on a Domain.
Alternatively, you can access the server at http://ipofthemachine:port
Make sure the port you are using is exposed if you have any firewall in place.

I can't understand Why I can only connect to 8080 port in node Js express server

const express = require('express');
const app = express();
app.set('port', process.env.PORT || '3000');
app.get('/', (req,res)=>{
res.send('Hello Express');
});
app.listen(app.get('port'),()=>{
console.log(app.get('port'),'listening');
})
Hi, I started studying nodeJs but got a problem.
If I run the server this way,
$ node app.js
8080 Listening
There's no problem in connecting to this server.
However, I got a problem if I run the server after changing the port.
$ PORT=3001 node app.js
3001 Listening
It seems working without problem but I can't connect to 3001 port. So I tried other ports but neither worked. I can't understand why only 8080 port is available.
I'm developing by using AWS cloud9 IDE and Ubuntu 18.04.5 LTS.

This site can’t be reached on Nodejs Express?

I'm totally new to Node and I tried to run a test site on a hosting centos 7 (vultr.com). I've got nodejs, express installed.
Hello.js
const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
run node hello.js
On my PC, http://x.x.x.x:3000/ => shows This site can’t be reached
x.x.x.x took too long to respond.
UPDATE:
I think you should consider about your server port. Have you open port 3000 in CentOS?
You can check your open port by typing
iptables -L
I think the firewall blocked your port you can open it by type this command
iptables -A INPUT -p tcp --dport 3000 -j ACCEPT
If you are using remote server, probably your 8080 port is blocked.
If you have root access and port 80 is open you can try and run script with sudo
But the first option is probably your problem
If you use the Google Cloud platform, you can open port 3000 at FIREWALL RULES in VPC network.
It works for me.

Nodejs port 8000 ,4000 not working on server

I am using nodejs for rest-api and my app running on 2000,3000 but when i use port 8000 it work on browser but it not working on iphone also i was opened port 8000 from whm.
Try to run your application on 0.0.0.0 ip address, this guarantees it would be listening on all the configured network interfaces
var server = app.listen(8000, '0.0.0.0', function () {
var host = server.address().address;
var port = server.address().port;
console.log('App listening at http://%s:%s', host, port);
});

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

Resources