Can't send requests from other devices to nodejs server - javascript

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.

Related

How to get ipv4 address of host in JavaScript/React

I currently have a react app and flask server running locally. The react app makes http requests to the flask server via axios (whenever a button is pressed for example). This is running on a raspberry pi.
I have been able to connect to the react app from other devices on the same network using my ipv4 address (e.g. http://10.32.24.152:3000). However, whenever I press a button, the react app will try to communicate with the flask server on localhost (which is the localhost of my other device, not the device that the react app is running on).
I think I would need to set the axios url to 10.32.24.152:5000 to make this work. I was wondering how to obtain the ipv4 IP address of the host dynamically in Javascript since I don't want to hardcode 10.32.24.152.
I figured it out. You can use window.location.host. This will be localhost if you access the website on the host directly, or it will be the host IP if you access the website from a different device.

How can I allow "Mixed Active Content" so that my website deployed on Firebase Hosting can make HTTP requests to my local server?

I've a React app deployed on Firebase Hosting. It calls a server running on another machine on the same network. The server uses a hardware radio to communicate with some other devices, so it must run on the machine with the radio attached to it.
The frontend sends requests to the server to communicate using its radio.
When I deploy the frontend on firebase and set the ip of the server, I get "Blocked loading mixed active content...".
As far as I know, this is because my server is not HTTPS.
How can I fix this? thank you

JavaScript Check for HTTP Server on Local Network

I am currently working on a device that would connect to a Wi-Fi network and then host an HTTP server on the local network that you can connect to and configure the device. The problem is that the only way to access the device would be through its local IP. I'd like to make a program in JavaScript that would automatically check each device on the local network and determine the local IP of the device hosting the HTTP server and then redirect to it. Is there a way to do this? According to this answer it is possible to detect if a port is open using something like <img src="http://192.168.1.1/favicon.ico" onload="alert('Yay')" onerror="alert('No')">. Is this a good way of achieving this, and how would I apply this to scanning an entire network? Thanks

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.

Locate server on LAN in JavaScript

TL;DR
In Javascript, how do you to find the IP of all servers running a specified program on a specified port in the LAN?
Background
I'm writing a server in node.js that is supposed to connect users browsers as controllers to a common device on which a game is running. The browsers are running a web app based on html and Javascript. The connection is based on socket.io.
I'd like for the web app to be able to find all available instances of this server in the LAN in order to create a server list for the user to choose from.
Is there a way to make the server discoverable by the web app on the local network in Javascript, and in that case: how?
Ideas
Have the server broadcast its IP to all devices on the LAN and have the web app listen for these messages (No idea how to do this without node on the client)
Connect to every IP on the network and see if the connection is successful. (Does not seem feasible)
Scan every IP on the network and connect only to those where the port is open. (Once again, no idea how to do this without node on the client and does not seeem feasible either.)
EDIT
The server is supposed to be portable and work independently, without any central system backing it up or providing matchmaking for clients. It is a LAN only server and should work even without internet access.
There is no way for you do this. Sorry. Since there is no exposure to UDP on client-side JavaScript, broadcasting is out of question. Any attempt on massive scanning will quickly raise flags on network monitoring software. You have to use a known address.

Categories

Resources