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

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.

Related

My NodeJS server is not working in remove server

When I run my NodeJS server in remote server with script "next" for example, it compiles successful and get "Running in localhost:3000" but when I try to enter to ip:3000 didn't get response from server.
What I should do?
Ubuntu server.
The server respond correctly without :3000
Have you tried to open the ports on your server for port 3000? :)
I'm on Ubuntu so this may be different on your system, but i think this should work for you.
ufw allow 3000
If you want to access remotely you cannot listen your server using localhost
Try to listen server in 0.0.0.0
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");
Debugging this might follow a path on these lines:
Verify the server is actually running on that machine. Try to connect to it locally from another shell on that that remote/server machine (use curl if the machine is headless). If it's not running locally, you clearly won't be able to connect remotely.
Assuming the service is actually running, determine if you have the right IP address for the remote machine. Running something like ifconfig (ipconfig on windows I think?) will tell you this.
Determine if you can reach the machine at all over the network. You might try to send it a ping, or connect to another service you know it's offering. If you can't reach it, is it running a firewall that's preventing you or is there address translation going on somewhere?

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

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