AWS public ip with port not working for nodejs - node.js

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.

Related

pm2 works fine on port 80, but no other ports are accessible from another computer in local network (connection refused)

I started pm2 server on my server running ubuntu 20.
To simplify the problem, I created a hello world app using node, like this (/var/www/html/pip/exampleserver.js):
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/');
After starting the server with 'pm2 start exampleserver.js', the server is started, and it works fine when I access it from localhost :
wget -qO- localhost:3000
Result : "Hello World"
But I get ERR_CONNECTION_REFUSED when accessing the page from another computer on the same network (from http://10.200.96.23:3000, either from browser or command line)
However, when I change the script to listen to port 80 and restart pm2, it works just fine from the other computer (from http://10.200.96.23). It shows the result "Hello World".
Here is the result of netstat and ufw status that i've disabled in advance :
netstat and ufw status
Screenshot of error connection refused :
ERR_CONNECTION_REFUSED
I tried other ports, but nothing works other than 80. I don't want to run this server on port 80. What could be the problem?
Port 80 is open by default but not for other ports.
#aRvi is correct, you might need to open port 300 for that to be accessible.
Also pm2 monitor will help you see code errors and monitor your application.
This saves my day many times when debugging my codes in PM2
First run
pm2 list
and delete the processes with
pm2 delete [ID]
Define a new ufw rule
ufw allow 3000/tcp
and run your application again.

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

Running a node.js server on my VPS on port 3000 and the connection times out

In hostgator I have a VPS running centOS. I installed NodeJS and screen.
I added the following code to a file named index.js:
//1
var http = require('http');
//2
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<html><body><h1>Hello World</h1></body></html>');
}).listen(3000);
console.log('Server running on port 3000.');
On 'screen:1' I run the following command:
node index.js
It gives me the console output stating 'Server running on port 3000.'
I switch to 'screen:0' and run the following command:
curl localhost:3000
and I get the following response:
<html><body><h1>Hello World</h1></body></html>
Yet, when I try my server's IP address (substitute the xxx for a real IP address, cause I'm not disclosing my VPS IP address):
xxx.xxx.xxx.xxx:3000
The page never comes up and eventually it times out.
I've tried various ports (8080, 7000) and to not avail.
Do I need to place the iOS project in a different directory.
Currently I have it in /root/Projects/NodeTutorial2/index.js.
What do I need to do to get a hello world response from my VPS?
If you're getting a response from on the box, but not from other boxes, it's almost certainly a firewall issue. Turning off IPTables or allowing the traffic in on the port in question is one option but an easier / more appropriate option is to simply have your app use port 80 (for HTTP) or 443 (for HTTPS). You can either do that by listening to that port on the app directly, or by having a web server that acts as a reverse-proxy for you (e.g. NGINX or Apache).

Can't connect to nodejs server

I run Apache on my server. Going to my address x.x.x.x:port loads the index.html page in /var/www. When I stop the server, I can no longer connect (all good).
Now I start the node server with node server.js (the server.js file below is also located in /var/www).
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(port, 'x.x.x.x');
console.log('Server running at http://x.x.x.x:port/');
This gives the error listen EADDRNOTAVAIL, but I am not running any other node server (there is no other process running at this port).
I have also tried omitting the IP address and just listening thus: listen(port);
This returns no errors, but I cannot connect to the server (Browser says: Firefox can't establish a connection to the server at x.x.x.x:p.)
I have found out the problem. You don't need to specify a host name:
listen(port, 'x.x.x.x')
should just be
listen(port)
otherwise the server will not accept any connection except ones directed at the specified ip.
The port is in use or not available. Try a different port like:
listen(88, 'x.x.x.x');
and see if that connects. Also, make sure that x.x.x.x is actually the ip address of your server. You can listen on all IPs by doing:
listen(88, '0.0.0.0');
or by leaving the host/ip section out entirely. If it does connect on another port, you just need to find what is using the port you want. If it's port 80, use:
sudo netstat -tulpn | grep :80
to get the program using that port.
Sounds like the port is locked up and in use..
The following command will give you a list of node processes running.
ps | grep node
To free up that port, stop the process using the following.
kill <processId>

node - what's wrong with my app?

I've got httpd running on port 80 and I'm trying to bind a node app to port 8080.
Here it is:
var server = require('http').createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
});
server.listen(8080);
Obviously have run it from ssh command line via
node myApp.js
But whenever I type "http://my-domain:8080/" in the browser it just hangs and gives me nothing..
I've tried a range of different ports and listening on hostname 0.0.0.0, all giving the same result.
Have run netstat as comments suggested and results is :
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 3894/node
I'm using centOS on nan unmanaged VPS!
EDIT: Looks like its a firewall issue, could someone point me in the right direction as to how to configure the firewalls for a CentOS VPS.. ?
Might be your firewall settings. In the shell prompt on the server, try connecting with curl.
curl -v http://localhost:8080/
If you can access it via the localhost but not via a browser then you most likely have a firewall issue.
If you can access the server via localhost then the next thing to do is to test the server from the outside via IP address. If you can access it via IP address then you have a DNS issue. If you cannot access it via IP address from the outside then you have firewall issue.
Firewall issues are platform specific. We'd need to know the platform to point you in the right direction.
try to write:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080);
console.log('Server running ');
the .listen(8080); is in the same line.
or to run it with localhost:8080/ from the server may he block from outside

Resources