Expo cant get data from json-server node.js - node.js

I recently started to learn react native. I have such a problem.
I launch the Expo application and it works I go to the subitem where the data is taken from the json server and there is empty. Writes a network request failed. As I understand data from the server is not read.
Here is more detailed.
First, run json server, type json-server --watch db.json -p 3001 at the command prompt. It all started. I’ll go into the browser and type localhos: 3001 / books. And the data appears. As I understand it answers the request.
Next, run react native. Everything loads, runs in the metro bundler browser (localhost: 19002).
There are three types of launching the program on the phone:
1.local and the address 127.0.0.1, as I understand it, it is needed to connect and run through the usb cable. I tried using this method the application starts. But the data does not appear. Writes a network request failed.
2.lan and the ip address of the computer, as I understand it through wi-fi. Since I don’t have a wifi router, I tried to give wifi from my computer. I connected a modem. And its began sharing wifi. I connected the phone and started it. It did not start at all. IP address of the computer in lan mode by default, takes IP address from the modem (192.168.8.100) and the phone (192.168.43.216). In short, the phone and the computer must be on the same local network. I think may be if I'll take another comp. It will be used as a Wi-Fi routet instead of a router. And then I’ll connect my working computer and phone to it. I configured ip addresses. Still not starting. I think that i did something wrong I am not an expert in local area networks. If you have any ideas, help.
3.tunnel did not quite understand how to use. I decided to share the wi-fi from the phone and make an access point. Connect my computer and ran. It seems to have started but still does not show data.
Please, help.
1) to form lan connection without wifi router.
2) how to read data from json server to the phone.
P.s. i killed 3 days trying to figure it out.

I copied the example from "Getting Started" in the README and then ran npx json-server --watch db.json -p 3001. I then went to http://localhost:3001/posts/1 in my browser and it worked as expected.
Next, I opened a new tab in terminal and ran npx ngrok http 3001 to expose create a tunnel to access the http server at port 3001 from anywhere. Once the tunnel started I saw the following information:
ngrok by #inconshreveable (Ctrl+C to quit)
Session Status online
Session Expires 7 hours, 58 minutes
Version 2.3.35
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://ef4b99ca.ngrok.io -> http://localhost:3001
Forwarding https://ef4b99ca.ngrok.io -> http://localhost:3001
I opened https://ef4b99ca.ngrok.io/posts/1 in my browser and it worked as expected (this will not work for you when you are reading this because I will have closed the ngrok tunnel and my json-server by then).
If you follow these steps and use the tunnel URL in your app you will be able to access it in development from your device.

Related

getting issues while trying to call api (local host) from real ios device(Mac OS)

first, I run the nodeJs server on localhost
then I tried to call API's from my real ios device, but I’m getting issues while connecting with local host
everything in working fine in case of android device and emulator
Try binding the backend on 0.0.0.0 if you want to test from a different device, but in the same network. The 0.0.0.0 will listen on all network interfaces where localhost might only listen to your local loopback net device.
You should also point to your network address where the backend is running from your iOS device.
You may find this other stack overflow question helpful.
That localhost URL would make the iPhone try to reach out to itself, you need to set that variable to an IP or URL that the iPhone can actually reach out to. You may find it helpful to use a tunnelling service like ngrok which will make an external URL that your server's localhost can be reached at.

Express server unreachable from browser when running in WSL2

So, I have been trying to start a basic express server from inside WSL2 on port 3000. Every time I go to the browser, it says that "localhost:3000" is unreachable. I'm not really sure where the problem is. I have been able to start and access react applications that are running on port 3000 in WSL2 without a problem. Additionally, I have been able to start Flask servers from other ports on my machine from within WSL2 without a problem. The issue somehow seems isolated to Express JS servers, which doesn't make a whole lot of sense to me.
I have confirmed that the app is exposed on all interfaces, so localhost should be reachable.
When starting the application, I have been monitoring it with netstat -ltp. The node process does show up in the output, which indicates that the port is listening.
Are there any weird WSL2 configuration steps that I missed. The strange part is that other applications that don't use Express JS, applications seem to be running perfectly fine on all ports.
I also already have all local ports accessible through the Windows Firewall and have made the ports accessible on the Windows side via this command: netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=127.0.0.x
I have also been testing this using npx express-generator your-project-name --no-view to create a standard Express JS template.
Update:
The server is reachable from the IP address on my network, but unreachable from localhost.

Why cant I access my react app from another device?

So im trying to access my React web application with ipadress and the portnumber 3000. I tried from another computer and from another phone. But the webpage keeps on loading and then tells me it can't reach the website. Why not?
Here is the github link for my project:
https://github.com/darja2001/collaborative-whiteboard-SR
Make Sure Your Computer and Phone or computer are connected to same
network.
Open command prompt and type ipconfig and Copy Ipv4 address.
In your phone or another computer open URL http://(Ipv4 adress):(port
number)
And you will get it
Else you can also use ngrok to access your react app anywhere.

If I use http-server with node can other devices on my network access it?

Background
I am building a site using angularjs and want to make sure that its displaying properly on my phone. I'm aware of the Google Chrome emulator but would like to show people it directly on the phone.
Node.JS
I'm using http-server . to run a server at the base of my app. This was the quickest and easiest way of getting a server for local testing.
I have tried going onto the same network via wifi on my phone and then trying to get to localhost:8080 but it just gives me a "this web page is not available" error
Question
My question is, is there a way to get my web app on my phone with http-server . or do I need to set up a proper web-server? I thought maybe it would just be simple to get it on any device attached to the same network.
Since our application(server) and devices(in which you want to access the application) running on same network.
You can access this with the help of IP address.
Let say, your app is running on
192.168.1.1:3000
So you can access it as localhost:3000 on local machine.
And as 192.168.1.1:3000 on devices which is using the same network.
any device can access to server. Use ip in device not use localhost
Use ngrok https://ngrok.com/ , ngrok expose your localhost behind a NAT or firewall to the internet , easy to use , after install just type on command line ;
ngrok 7070
then it gives you a link like ; 3g87g9g.ngrok.com
Use ifconfig utility if you are in linux or use any other tool to get the ip address of your phone (Web server), for example 192.168.1.10 is IP address of the phone.
You can access the web page from any other device in the network by going to 192.168.1.10:8080 in the browser.
Note that 8080 is the port to your server application, you select the port from node.js

Does node.js server need internet connection to run?

I disable the internet connection and run the node server npm start, then it throws the error:
And now enabling internet connection and running the server works fine:
So, I want to confirm if this really needs to be connected to the internet while starting the server?
If it really needs internet connection, then is there any idea to run the server offline?
If I connect to the internet and run the server and there after plug out the internet connection then there's nothing goes wrong. Everything works fine. I don't know why just running the server requires the internet connection.
OMG! It's really cool! I got it working offline after hard research in my code using 127.0.0.1 instead of localhost for mongo uri.
Using localhost needed for me to connect to the internet.
And using 127.0.0.1 worked in both condition i.e. with internet connection and without internet connection.
But, really I'm totally amazed of this.
I found the really cool topic on this which made me clear about this.
If you use 127.0.0.1, then (intelligent) software will just turn that directly into an IP address and use it.
And there's no guarantee that your hosts file will actually be used for that resolution (first, or at all) so localhost may become a totally different IP address.

Resources