Node server not reachable from outside - node.js

I am playing with a small node server application.I have hosted it in AWS Lightsail's ubuntu instance. It is reachable from local browser like http://localhost:4201/
but when I try to access it from remote, it is unreachable.
In aws instance's network config I have opened traffic for all ports
I have cleared all rules from iptables as well. I am able to reach http port 80 and ping successfully. But no luck with node server, what am I missing? Is there a special way to enable traffic to node server?

I debugged it, basically in server.ts
I had bound it to localhost so it won't accept request from outside
app.listen(4201, '127.0.0.1', function () {
console.log('Server Listening on 0.0.0.0:4201');
});
changed it to 0.0.0.0
app.listen(4201, '0.0.0.0', function () {
console.log('Server Listening on 0.0.0.0:4201');
});
And now it is accessible from everywhere.

Related

Trouble conecting to Node Server on EC2 Instance

I have a Node application running on an EC2 instance, and after npm start, I am unable to connect to the application via the web, but everything indicates that it is running correctly.
Here's how my server.js looks like:
app.listen(8000,"0.0.0.0", () => {
console.log("app running on port 8000");
});
How are the inbound rules of the EC2 security group:
I saw on other sites that removing " 0.0.0.0. " From app.listen and leaving only the port would solve the problem, but unfortunately that didn't work.
Another thing I tried was to add a * Custom TCP * rule to port 8000 in the security group, but this prevented the application from running, saying that the port was already in use
Something is already listening on port 8000, a netstat should tell you what it is.
Simply add a TCP security group for port 8001 or 8080 and in your code use that port instead.

Unable to access localhost:3000 from other machine

I'm having a node.js application running on localhost:3000 and able to access it through my browser. I'm having the following configuration in my server.js file.
here port is 3000
app.listen(port, '0.0.0.0', function onStart(err) {
if (err) {
console.log(err);
}
console.info('==> 🌎 Listening on port %s. Open up http://localhost:%s/ in your browser.', port);
});
When I try to access my server using my laptop's IP by checking it through what's my ip on google e.g. ip is http://58.27.123.12:3000, it doesn't take me to the requested page. I'm running the application on Ubuntu 19.10.
I'm also able to ping my ip through other systems and it responds successfully. What I might be doing wrong? Thanks
if you want people to access on LAN give them your localIP with port number or if you want to provide access via internet check below links:-
https://localhost.run/
https://localtunnel.github.io/www/
ssh -R 80:localhost:3000 ssh.localhost.run
using this command solved the issue after installing localtunnel

How to access meteor app from outside without passing through NginX?

I am hosting a meteor app on an Ubunu Linux machine. The app is listening on port 3000. If I use a webserver, like NginX and forwards the HTTP requests from port 80 to 3000 I can browse to the server from the outside and see reach the app. However, when I try to access the app directly at port 3000, i.e. browse http://myhost:3000 it just tries to connect and nothing happens.
I have made sure that all firewalls are down and that the app is listening on all interfaces, i.e. 0.0.0.0:3000, so that is not the issue.
To verify that port was actually reachable, I created a simple node js webserver:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Hello World!');
res.end();
}).listen(3000);
Now browsing to the the sever, I can see "Hello World!". So obviously this works so why I can not reach meteor has nothing to do with firewalls or unopened ports.
Thus it seems that there is something strange when trying to access a meteor app directly at port 3000. But why? I use the following environment variables:
export MONGO_URL=mongodb://localhost:27017/meteor
export HOST=myhost
export PORT=3000
export ROOT_URL=http://myhost
So what am I missing? Ports are open and I can see that the node process instance is listening on port 3000 when I run netstat -tulpan
I was using the force-ssl meteor package which makes a redirect back to the ROOT_URL without port number. So solution is to remove the package to make it work with a custom port.
I was discussing the solution on the meteor forum where I got the solution:
https://forums.meteor.com/t/can-not-access-meteor-app-without-passing-through-nginx-server/40739/11

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

Node.js and Apache: connection issues

I have installed Node.js with Socket.io on a CentOS server which is running Apache on port 80.
I created a socket test, which justs listens on port 8080.
If I curl the address localhost:8080 from within the server's shell, I get the Socket.io-welcome message. If I have a line like this:
<script src="http://localhost:8080/socket.io/socket.io.js"></script>
Then the browser cannot find the file.
A "solution" was to proxy requests to /nodejs/ to http://localhost:8080/, but this solution did not work for very long.
Is it possible to run the Node.js server when we have Apache installed? Which settings must be changed in order for us to access the url: http://server.com:8080 ? It seems the Node.js only accepts connections from localhost.
Problem is most probably in your node.js program.
It should listen on 0.0.0.0 and not 127.0.0.1 which is local only.
So where you've got something like:
.listen(8080, '127.0.0.1'); // '127.0.0.1' or 'localhost'
You should change it to:
.listen(8080); // or 0.0.0.0
Apache will only interfere if it also uses port 8080 but you should get an error when starting your node app if this is the case.
Also, if you connect to http://localhost in your browser, it will only work if the server is on the same local machine as the browser. Fine for testing I guess.
You'll have to connect to a domain or ip address if you have a hosted server else no browser will find it.
Update:
Your socket.io code also needs to connect correctly:
var socket = io.connect('http://correct.server.com:8080'); // not localhost
and your browser needs to load the javascript file from the correct place:
<script src="http://correct.server.com/socket.io/socket.io.js"></script> // not localhost
This might help with firewall / load balancer issues:
https://github.com/LearnBoost/socket.io/wiki/Socket.IO-and-firewall-software

Resources