Rest API w/ JsonServer in Ubuntu - Connection Refused - linux

I was requested to create a Web Server with an API in it to test firewall rule we are planning to implement soon. I am quite new with all this network stuff, so I'm a bit lost on it.
I need to call this API from another machine. The JsonServer is using my port 3000, when I request my API through Postman, using https://localhost:3000/posts, my JSON file is returned normally, however when I change the localhost, to my IP, my connection is being refused. I decided to install a WebServer, to test it and I could access the default Apache page from my others machines. I opened the port 3000 on my server, but I couldn't. The WebServer is working with my IP as well. It seems to be something with my port 3000, and I a not sure about how to fix that.
I executed the command:
sudo lsof -i -P -n | grep 3000
And this was my output
node 29472 user 22u IPv4 254348 0t0 TCP 127.0.0.1:3000 (LISTEN)
Any ideas how can I access my API from another machine?

Related

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

I can only access NodeJS server locally

My server's hosted at DigitalOcean (it's a droplet) and basically, I cannot access my NodeJS app via Internet, only server-side. It's running on port 9000, I've allowed traffic to the port via ufw and iptables, no luck. When I run curl || wget while SSH-ed to the server, I get a normal response as if everything's in order. But when I try to access the server from an another machine, I just get timed out because the server returns nothing. I've heard DigitalOcean sometimes disable connections to all ports except ssh,www and ssl, but I think I've successfully 'opened' them. Any suggestions?
This is what I get when I run netstat -tulp | grep LISTEN
Turns out my dashboard was all messed up when it comes to ports, which I forgot to check, of course, so opening them directly on the server gave no results whatsoever.

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!

How can I find where node.js is running?

I have a VPS with node.js installed, I already uploaded a basic example to test it on the server, so I tried doing this:
I access by SSH, navigate to my project folder and run
node app.js
I get this message
Express server listening on port 8080
I thought i could see my app here
example.com:8080 or server.example:8080... but nothing. Then I tried with the info from os.networkInterfaces(); and os.host(); and still nothing happen
could you help me out? as you can see I am a total noob on node.js. What I'm doing wrong? or what should I do before running my app? Something related to DNS's? i have no idea
How do you ssh to your host? with ip or name? Is it something like:
ssh root#example.com
if so then at least you know your DNS is ok.
Once on the server do a
netstat -a
if you find *:8080 then your server is listening in the default ip. If you see something like 12.23.45.67:8080 then this number is the ip your server is listening.
ifconfig
will give you the servers ip. This should be the same as the ip of example.com. If not then maybe there is some router/firewall in front of your server and you have to configure that to allow port 8080 to reach your server.
if someone ever has the same problem this is how i solved on CentOS:
Open this file
/ Etc / csf / csf.conf
Add the required port
Allow incoming TCP ports
TCP_IN = "20,21,22,25,53,80,110,143,443,465,587,993,995,26"
Allow outgoing TCP ports
TCP_OUT = "20,21,22,25,37,43,53,80,110,113,443,587,873"
Restart
# # Csf-r

Enable HTTP TCP connection requests in Arch Linux for neo4j

My laptop is running a local neo4j server. I can use it with localhost:7474 but when i try connecting it with 192.168.1.12:7474 it is unreacheable.
Turns out linux is blocking connections other than web server port 80. Because i can access my Apache server on 192.168.1.12/
I am trying to allow TCP connections on port 7474 by using
iptables -A TCP -p tcp --dport 7474 -j ACCEPT
but it gives a response as -
iptables: No chain/target/match by that name.
How can i make other clients access neo4j server running at my laptop on port 7474. My laptop IP addr is 192.168.1.12.
I doubt that it is blocking it. Probably your neo4j server is only running at 127.0.0.1. You can check this out with netstat -nplt: you will probably see something (the apache) listening on 0.0.0.0:80 or :::80 (e.g. catchall address) but on port 7474 you will probably only see 127.0.0.1:7474 or ::1:7474. If this is the case you need to reconfigure your neo4j server to listen not only on localhost (don't know how, checkout the documentation).
Okay. I had uncommented the webserver address line but it still wasn't working.
So i reinstalled neo4j. That solved it. Weird but worked.

Resources