Home > database >  Unable to make a GET request with React Native on physical device
Unable to make a GET request with React Native on physical device

Time:02-01

My setup: The server is running locally on my machine, and I have the React Native app running on my physical phone. Here is the code snippet for the mobile app:

import React, {useEffect, useState} from 'react';
import {View, Text, StyleSheet, StatusBar} from 'react-native'

import axios from 'axios'

export default function App() {
    const [dkc, setDkc] = useState([]);

    useEffect(() => {
        axios.get('http://192.168.10.104:3333/dkc')
            .then(response => {
                setDkc(response.data); //Doesn't goes here
            })
            .catch(error => {
                console.log(`Error: ${error.message}`); //Goes here [Network Error]
            ;
        }, []);
    });

    //Code for the app style

};

And here is the code snippet for the server:

const express = require('express');
const cors = require('cors');

const app = express();
app.use(express.json());
app.use(cors())

function preRequisicao(req, res, next){
    const {url, method} = req;
    console.log(`[${method.toUpperCase()}] ${url}`);
    return next();
}

app.get("/", preRequisicao, (req, res) => {
    return res.json({ message: "ok" });
});

app.get("/dkc", (req, res) => {
    return res.json({ character: ["donkey kong", "diddy kong", "dixie kong", "kiddy kong"]});
});

app.listen(3333, () => {
    console.log("           
  •  Tags:  
  • Related