[英]Express Server cannot GET request from the React Native app
[英]How do you send a request to an express server from a react native app?
我有2個單獨的應用程序,一個react-native
應用程序和一個express
后端服務器。 我使用express-generator
創建了服務器。 我的服務器在users.js
具有以下代碼
router.get('/', function(req, res, next) {
res.json([{
id: 1,
username: "foo"
}, {
id: 2,
username: "bar"
}]);
});
服務器設置為偵聽端口8080並具有app.use('/users', users)
。 基本上,只有express-generator
隨附的所有代碼。
在我的本機應用程序中,我有: "proxy": "http://localhost:8080"
然后在App.js
:
componentDidMount() {
fetch('/users'
.then(res => res.json())
.then(users => this.setState({ users }))
}
我正在使用Expo將我的應用程序加載到Android設備上,並且得到: unexpected url: /users
。 如果我在瀏覽器中訪問localhost:8080
,則數據可見,因此我認為問題必須出在客戶端。
發出請求時,您需要添加完整的主機名和端口號。 在App.js中以http:// localhost:8080 / users獲取數據時,您需要編寫完整的url。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.