skip to Main Content

How to open http on expo react native app

I’m designing an Android app using Expo. I’m struggling with opening a localhost port on the app to create a local API route on the Android device itself. Could you please help me with this? Thank you.

2

Answers


  1. Update Expo configuration:
    You need to specify the IP address and port of your local server in the Expo configuration. This can be done in your app.json or app.config.js file.

    {
      "expo": {
        "packagerOpts": {
          "hostType": "lan",
          "lanType": "ip",
          "lanIp": "<YOUR_LOCAL_IP_ADDRESS>"
        }
      }
    }
    

    Update your API calls:
    Update the API endpoint URLs in your React Native code to use the IP address and port of your local server.

    const API_URL = ‘http://<YOUR_LOCAL_IP_ADDRESS>:/api’;

    Configure your local server:

    const express = require('express');
    const app = express();
    const PORT = 3000;
    
    // Allow requests from any origin
    app.use((req, res, next) => {
      res.setHeader('Access-Control-Allow-Origin', '*');
      next();
    });
    
    // Your API routes
    app.get('/api', (req, res) => {
      // Handle API logic
    });
    
    app.listen(PORT, () => {
      console.log(`Server is running on http://localhost:${PORT}`);
    });
    
    Login or Signup to reply.
  2. ive suferd from this befor,all u had to do is to change your sever localhost to be the same as mobile and mustt be your network localhost,for example from your pc go to your connected wifi settings and show details you will find wifi localhost that you must run ur sever and api with it.
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search