How to get ipv4 address of host in JavaScript/React - javascript

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.

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.

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

Is there a way to fetch Apache environment variables in React Js?

I am serving the react application through Apache. For SSO (Single Sign On) , I am using Apache based authentication. So the flow is whenever a user hitting the application URL , Apache will take care of getting the user information from SSO server and then it will redirect to home page. now I am having user information's in Apache response headers. Is there a way we can access these information in react js.
Note : I am not using any middleware application layer. Only React Js application.

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.

Sending http request to local server using appcelerator

I am developing mobile apps with appcelerator. My problem is, I am not able to calling REST web service using Android emulator i.e server is in my local system
I am getting "not found" error message, also I am using my local IPV4 Address in emulator, but no luck.
I think you need to access your REST services from your local server from your emulator.
So if you are using your REST services using localhost then try as follow.
If your webservice URL as below,
http://www.example.com/YOUR_WEB_SERVICE_NAME
Then replace www.example.com with 10.0.2.2 because you are running your application from emulator, so you will have to use your localserver using 10.0.2.2.

Categories

Resources