kev
kev

Reputation: 75

How to Setup Proxy Target in React Native with Nodejs?

Trying to configure proxy in react native with node to run axios calls.

Tried the following code in server/package.json

"proxy": {
    "/*": {
      "target": "http://localhost:5000/"
    }
  },
  "scripts": {
    "start": "node index.js",
    "server": "nodemon index.js",
    "client": "cd ../client && yarn ios",
    "dev": "concurrently \"yarn server\" \"yarn client\""
  }

server/authRouter.js

const authRouter = require('express').Router();
authRouter.get('/test', (req, res) => {
    res.send('proxy success');
  });

module.exports = authRouter;

server/index.js

const express = require('express');
const authRouter = require('./authRouter');
const app = express();
app.use('/auth', authRouter);
const PORT = process.env.PORT || 5000;
app.listen(PORT);

client/app.js

await axios.get('/auth/test');

When I run yarn dev and test an axios call, it logs the following error

 LOG      [Error: Network Error]

any help would be much appreciated.

Upvotes: 2

Views: 1670

Answers (1)

Saurabh Singh
Saurabh Singh

Reputation: 52

Try calling the proxy directly in axios http://localhost:5000/auth/test

I'm not sure why it doesn't work that way, even i had a problem in the past.

Upvotes: 1

Related Questions