Problem With Local Area Connection MONGO DB - javascript

I am developing an application that used mongo db as the database - server.
the server resides on my personal machine , and wanted my friend to be able to access it.
I am using an ethernet cable to connect to my friends machine.
changed mongodb.cfg file and added his IP address and restarted the service which work fine on my local machine.
but when ever he tries to connect he get an error stating "target-machine-actively-refused-it on 192.168.2.4:2707
using the command " mongo --host 192.168.2.4:2707
Have checked the port and all seems to be fine and disabled firewall. but nothing seems to work
he can ping my address , but when it comes to the access of db it refuses. any help please
tried netstat on my machines
deliberately closed my firewall
-changed the cross over cable , but still he can only ping my machine

To allow access from specific IP you can set bindIp property.
net:
port: 27017 // or any other custom port
bindIp: 127.0.0.1,192.168.0.10,192.168.0.12
https://gist.github.com/royz/46397fe4ee25dc14418b41821ee45335

Related

Can't send requests from other devices to nodejs server

I am using a React client and a node js server (MERN Stack). The server runs good on my pc but when i try to connect from my mobile phone to the ipv4 of my pc with the correct port I am unable to send requests to the server. I tried to change the proxy of the React app but it doesn't seem to work.
Do you know what the problem might be?
Thanks.
That could be a network issue if your are trying to access your PC from an external IP (Phone not connected to local network, wifi), for example the PC connected to a router that does not forward external traffic to it.
If you can, try connecting your phone to the same network via wifi and access the local IP:PORT instead of ipv4.

JSON Data not appear in another computer

I tried to fetch JSON data from the API with http://localhost:3030/get/articles and the data was successfully displayed on my reactjs, but when I tried on another computer the JSON data did not appear, I used axios to get the JSON data. Has anyone ever experienced or been able to help my problem?
Localhost is just a local server built on your own computer machine. Which can be used to host website locally for testing it.
If you want your project to be accessible from other computer, you should install your project on a web server with registered domain name pointing to this web server.
Localhost is a local server built on your own computer to development and testing. so if you want to access your application another users, you should published to server side computer or cloud.
The problem here is that you're accessing localhost which means the local machine.
When you try to access localhost in your web browser, you're basically saying this computer. If you do it on the machine where you develop your application then it will work but on another computer it wont.
A solution to access your JSON from another machine could be to use the IP of the machine that provide the JSON instead of the localhost keyword.
Like this: http://IP_OF_THE_MACHINE:3030/get/articles.
E.g.
http://192.168.10.12:3030/get/articles
You can get the machine's IP by typing ipconfig (for Windows) in a Command Prompt.
You can use a DNS entry or edit the C:\WINDOWS\system32\drivers\etc\hosts file on Windows to avoid typing the IP address everytime.
You can run ssh -R 80:localhost:8080 ssh.localhost.run. where 8080 is your port number. Yours should be along the lines ssh -R 80:localhost:3030 ssh.localhost.run since 3030 is your port number.
When you run this in your terminal you will get a message Connect to http://dummydata.com.localhost.run. Go to that link and basically you'll get your stuff online.

How can we access Node server outside the network if the app is listening on 'localhost'?

Using Express when we use app.listen(port) , the app location is localhost:port/
In local machine I completely understand how we can access to this address as we use a local browser running on same machine . Even other clients running on same network can access the server.
As per my knowledge localhost or 127.0.0.1 IP can be accessed on same or other machines in same network.
But if we deploy to cloud like Heroku without adding IP option like app.listen(port, IP_ADDRESS) instead we use app.listen(port), the only thing that varies is PORT number(process.env.PORT) but IP is still localhost. So how can clients from other networks access the server?
You can use port forwarding on your router to forward router_ip:port to local_ip:port allowing you to access it externaly
Just because you are connecting to your local instance via localhost doesn't mean it is not also exposed via IP. Localhost basically says don't resolve any IP, just loop back to this computer, but your node server will still be deployed to an actual IP address. Try looking up your computer's IP address and connecting to your node server through that instead of localhost, and you'll find you're still able to communicate with the server.
When deploying to a cloud service, or any other hosting service, you'll be given an IP address associated with that instance which is what will be used for resolving. Heroku in particular will blackbox a lot of the domain-space and port-forwarding process for you.

Nodejs connection bug with ip redirection

I have a problem with NodeJS (I think) using a kafka node on a node-red instance installed on a RPI3.
Scenario:
I have a cluster with a running instance of Kafka. The real ip of the machine which host the kafka broker is private so I have a public ip with a public port which I can use. Then, the requests are redirected to the broker.
Testing my environment with a broker installed on my laptop (so knowing the real ip of the broker) everything works fine.
Performing an attempt on the real cluster node-red shows the problem described in the terminal:
So, I took a look in js file representing the kafkaNode I used and I find this:
I'm quite sure the problem is in these line and in the use of the ip redirection. Anyway, honestly I'm a newbie of Nodejs and javascript so I don't know if there are some bugs about use of it.
Any ideas?
P.S.: I'm sure that kafka broker is correctly running and installed. the problem is exactly in js.
I also tried to reach the "fake ip" with telnet, and it works fine.
Thanks in advance
Kafka does not work with default values configured if you use a NAT, VM, or other proxy for "ip redirection" because the clients dynamically discover the private IPs of the real kafka brokers and will try to connect directly to those IP addresses and not just the one you configure in your client for the initial connection and meta-data request.
You need to make sure that the broker is setup to advertise the hostname or IP of the machine doing the redirection or your producers will not work.

php connecting to mysql on localhost, request from remote

I have a wamp server running on my local machine that is exposed to the outside world. I have several php scripts that allow an android app to access information contained in a mySQL database running on the same machine as the WAMP server. When tested from inside my local network everything works fine. However when I try to run the php scripts from outside my local network there appears to be an error connecting to mySQL.
connect.php
<?php
header('Access-Control-Allow-Origin: *');
/*** mysql hostname ***/
$hostname = '127.0.0.1';
/*** mysql username ***/
$username = 'chaosjr_user';
/*** mysql password ***/
$password = 'pass';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=chaos_jr", $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
I'm almost certain it has something to do with the fact I'm referencing localhost from the outside world, but when I replace 127.0.0.1 with the IP address on my internal network or the external IP address it doesn't work from anywhere. Any suggestions?
EDIT:
Just to clarify, I can get to my php scripts from the outside (I do have a static IP address) and get a response from them as long as that particular script does't have to query MySQL. For example I can connect to XXX.XXX.XXX.XXX/myPHPscript.php just fine and everything looks good. But menus that are supposed to populate with SQL data are empty.
You are correct - the address 127.0.0.1 is your local IP address and not the one you would connect with from the outside world.
If you have a static IP address, you'll need to use that. You may find things tricky if you don't have a fixed IP address, or if you have a firewall that blocks the port you are using.
You should still use 127.0.0.1 to connect to the DB since the PHP script and the DB runs in the same server as far as I understood. It doesn't care if your script is called from the outside, if the script and the DB are in the same server, 127.0.0.1 is the correct address.
In your PHP code it should stay 127.0.0.1 because the PHP and the MySQL run on the same server, which is your localhost at that IP. But contacting it is a lot harder.
Okay, so your local host is running on your computer right, so this can be reached by contacting 127.0.0.1 from that same computer only.
If you try to contact it from another device, you have to look up the private IP address of the device that you want to contact. This can be something like 192.168.x.x in most cases.
Now contacting this computer from outside your network is even harder because you will connect to your router first (via its public IP), and that router should forward you to your pc on a certain port (probably port 80). You have to make sure that you configured your router so it can forward to the correct computer in your network and to the correct port on that computer.
But wait, there's more! Most ISPs (internet service providers) change your public IP address every few days for security reasons, making it even harder to maintain the right reference to this computer you're trying to reach.
You can find your WAN IP (public IP) by visiting http://www.whatismyip.com and check your local IP by executing "ifconfig" in the shell, or "ipconfig" in the windows command prompt
If you do want your database to be available from outside your network (please be aware that your machine will need to be powered on at all times in order for the database to be available), you will need to configure a so called port forward on your router to your internal address. Then in your app, make sure to use your external IP address, your router will then reroute the request to your machine.
Also be aware that your MySQL will need to accept external connections, by default it binds to your localhost and will not listen to remote connections. This is because localhost is bound to a special loopback interface of your OS and is not actually bound to the network card in your computer. You will need to bind MySQL to the IP address that is bound to the network card, so it can communicate to the outside world.
In other words, what you want is possible, but takes many different steps to work, mainly network/access related and how to configure this depends on your setup. So there's no ready-to-go answer for this, it going to take some trial and error. But basically it comes down to these steps:
Configure a port forward for your MySQL port (default: tcp 3306) on your router.
Make sure that your MySQL server is bound to your "external" (in most LAN's this is a 192.168.0.x address) IP address, so not to localhost or 127.0.0.1 (this is a setting in the my.ini configuration file).
Make sure your app attempts a database on your remote IP address (the one you got from your ISP, so not your 127.0.0.1 or 192.168.0.x address!).
For the last step, it's also important that your ISP has given you a static IP adress. Many ISP's use DHCP adresses, meaning every time your modem resets, your IP address can be different when it boots up again. You can verify this with your ISP. Always remember that hosting stuff in-house is usually not ideal, it's OK for micro-projects or startup projects, but consider off-site hosting if things get serious.

Categories

Resources