最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Native fetch a local server using a proxy - Stack Overflow

programmeradmin8浏览0评论

In my react native app I am trying to perform a fetch to my local backend server. In my package.json I have put "proxy": "http://localhost:3000".

My fetch looks like:

fetch('/')
  .then(response => response.json())
  .then(json => console.log(json))
  .catch(err => console.log)

It catches an error

[TypeError: Network request failed]


When I remove the proxy and manually enter the address in the fetch it works and my server receives a GET request.

fetch('http://localhost:3000')
  .then(response => response.json())
  .then(json => console.log(json))
  .catch(err => console.log)

In my react native app I am trying to perform a fetch to my local backend server. In my package.json I have put "proxy": "http://localhost:3000".

My fetch looks like:

fetch('/')
  .then(response => response.json())
  .then(json => console.log(json))
  .catch(err => console.log)

It catches an error

[TypeError: Network request failed]


When I remove the proxy and manually enter the address in the fetch it works and my server receives a GET request.

fetch('http://localhost:3000')
  .then(response => response.json())
  .then(json => console.log(json))
  .catch(err => console.log)
Share Improve this question edited Jul 28, 2023 at 16:11 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 23, 2019 at 9:01 siddrajpootsiddrajpoot 1831 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Two points here:

  • Set proxy in package.json is a feature of create-react-app, which wraps webpack dev server's proxy. react-native uses Metro instead of Webpack as bundler and it does not support setting up a local proxy.

  • Different from the web, http client in react-native has no current host. So if you use fetch('/'), it won't know which domain or ip address to send request to.

hello you can use your network IP Address for using local server like instead localhost change with your Ip address and you can call your server e.g, http://192.xxx.x.xxx:3000

发布评论

评论列表(0)

  1. 暂无评论