Send get request to local api from react native mobile project - javascript

I am developing a mobile app using react native, SQL Server and NodeJS.
I got an API in my localhost. I run my application on an emulator and try to get request from API. But I got this message in catch(error):
[Error:Network Error].
Can somebody help me please.

Actually you are unable to access the local development server because port hasn't been forwarded by ADB yet. Try to replace the "localhost" with your machine IPv4 address and check else you can also try map your local server port just like React Native does.
To forward a port using ADB run the following command in your terminal
adb reverse tcp:8163 tcp:8163 remember this only works if you are using Android 6.0+.

Related

Why can't I get my android emulator to connect to my local server?

I am developing an app using React Native and made my server with node.js. For websocket connections I use socket.io
The thing is if I install the app to my android device and my android emulator, only my android device can connect to my local server. They both run the same code. I checked and the emulator has an active internet connection. Weird thing is I have an other app inside the emulator that connects to the same server without any problems using the same connection methods.
Do you guys have any idea why there is a problem like this?
Android emulators have a built in IP to use when connecting to something on your local machine.
If the socket is running on localhost, you want to connect to using the IP address 10.0.2.2 in the emulator.

ReactJS could not proxy request from localhost:3000 to localhost:3003

I am writing a SPA using create-react-app and am using an expressjs server as my backend. For development I run my frontend test server on port 3000 and my backend expressjs test server on 3003.
I make many API calls client side using Axios so for development convenience I have defined proxy: "http://localhost:3003" in my package.json
This works fine on my laptop, but when running on my workstation I constantly get the error when accessing my app:
Proxy error: Could not proxy request /sockjs-node/487/wrst1bub/websocket from localhost:3000 to http://localhost:3003/. See https://nodejs.org/api/errors.html#errors_common_system_errors for more information (ECONNRESET).
I have no idea where this is coming from and I do not use sockjs in any capacity as far as I know. This is a console error and it does not crash my app but it is very annoying and I would like to get rid of it. Any help is greatly appreciated.
In my case, I was using Firefox and for some reasons, it gives me the same error. I tried to use Chrome and it worked!

How to do a reverse proxy with node.js for a shiny app

EDIT
I think the problem might be that the WebSocket connection does not go through the proxy node.js. How to authorize the connection of the WebSocket?
I have a web app hosted on a nodeJS server. On the same vm I also have a shiny serveur hosting an app. I use node to redirect traffic to port 3838 (shiny) when a somes URL are requested.
I use this code on node :
app.use('/the-shiny-app', proxy({target: 'http://localhost:3838', changeOrigin: true}));
With this setting everything works fine on the shiny app when I go on mydomain/the-shiny-app/* except when I try to run code in a code box.
When I try to run code I get this error on the chrome console :
Connection closed. Info: {"type":"close","code":4503,"reason":"The application unexpectedly exited","wasClean":true}
An example of what I mean by code box :
if I do not use node.js and I redirect the traffic (on OS level) from port 80 directly to 3838 everything works fine.
sudo iptables -A PREROUTING -t nat -p tcp --dport 80 -j REDIRECT --to-ports 3838
I do not know exactly what kind of exchange is made between the browser and the shiny server when we run code, but maybe the protocol used is blocked by node.js.
I found the problem. As Antony Gibbs said you need to setup a WebSocket upgrade. I'm using http-proxy-middleware you cans find the doc here : https://github.com/chimurai/http-proxy-middleware

Creating a react-native app for IOS and I have created a Microsoft SQL server, how to connect these two?

I have an app built on react native and I want to connect to a database that i have made on the Microsoft SQL server. I have searched online and there are things like myssql but I still get errors with that. I followed this tutorial nodejs from the official Microsoft website, and it works with the nodejs and I use node to run it, but how can i use this in my react native project?
You can use AXIOS to make requests to your NodeJS Server
In Android you can access your localhost by the IP 10.0.2.2 (default proxy configured in your Android Virtual Device).
In iOS you can just use localhost
Remember to add the port assigned your NodeJS Server and the
protocol http
Example URL for Android: http://10.0.2.2:3000
You need to write back-end service code.I suggest you to use Asp.Net Web API .You can use Entity Framework.
Also you can use PHP,Java or Python...

Meteor testing app using local server and custom ip

I'm developing an meteor application client/server and app, too.
when i compile my application using this command:
meteor run ios-device --mobile-server 192.168.2.126
App on device cannot connect to server (my server is on local network) and return this error in javascrip console:
Failed to load resource: Could not connect to the server.
If i use:
meteor run ios-device
it works perfecly.
Why?
I need to have application installed on different server in different network. The server has always the same ip: 192.168.2.126.. I need my client/app application works when is connected to one of my local network..
Solved, i have to use this format per server url:
http://192.168.2.126:3000

Categories