[英]How do you send a request to an express server from a react native app?
[英]Express Server cannot GET request from the React Native app
目标:创建一个可以相互通信的前端和后端。 我从前端开始,使用https://reactnative.dev/movies.json
作为占位符来提供前端数据。 一旦成功完成,我就开始开发服务器,即使它在 Postman 中工作,它在前端也不能工作。
Issue: When I replace https://reactnative.dev/movies.json
with http://xxx.xxx.xxx.x:5000/movies.json
the backend and the frontend fail to communicate.
到目前为止的测试:我的 react native 应用程序可以成功GET
由https://reactnative.dev/movies.json
构建的 reactnative.dev JSON 电影列表,并将显示电影列表。
I have tested the backend with Postman and have verified that http://xxx.xxx.xxx.x:5000/movies.json
returns the exact same JSON response as https://reactnative.dev/movies.json
前端(作品 - 可以从GET
https://reactnative.dev/movies.json
):
import React from 'react';
import { StyleSheet, Text, View, ActivityIndicator, Button } from 'react-native';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
isLoading: true,
dataSource: null,
};
}
componentDidMount(){
return fetch('https://reactnative.dev/movies.json')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson.movies,
})
})
.catch((error) => {
console.log(error);
});
}
render () {
if(this.state.isLoading) {
return (
<View style={styles.container}>
<ActivityIndicator/>
</View>
)
} else {
let movies = this.state.dataSource.map((val, key) => {
return <View key={key} style={styles.item}>
<Text>{val.title}</Text>
</View>
});
return (
<View style={styles.container}>
{movies}
</View>
)
}
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
服务器结构:
/backend
--/data
--movies.json
--/routes
--movies.js
--routes.js
server.js
package.json
package-lock.json
server.cert
server.key
.env
.gitignore
/data/movies.json
{
"title": "The Basics - Networking",
"description": "Your app fetched this from a remote endpoint!",
"movies": [
{ "id": "1", "title": "Star Wars", "releaseYear": "1977" },
{ "id": "2", "title": "Back to the Future", "releaseYear": "1985" },
{ "id": "3", "title": "The Matrix", "releaseYear": "1999" },
{ "id": "4", "title": "Inception", "releaseYear": "2010" },
{ "id": "5", "title": "Interstellar", "releaseYear": "2014" }
]
}
/routes/movies.js
const userRoutes = (app, fs) => {
// variables
const dataPath = "./data/movies.json";
// READ
app.get("/movies.json", (req, res) => {
fs.readFile(dataPath, "utf8", (err, data) => {
if (err) {
throw err;
}
res.send(JSON.parse(data));
});
});
};
module.exports = userRoutes;
/routes/routes.js
// load up our shiny new route for users
const userRoutes = require("./movies");
const appRouter = (app, fs) => {
// we've added in a default route here that handles empty routes
// at the base API url
app.get("/", (req, res) => {
res.send("welcome to the development api-server");
});
// run our user route module here to complete the wire up
userRoutes(app, fs);
};
// this line is unchanged
module.exports = appRouter;
服务器.js
// load up the express framework and body-parser helper
const express = require("express");
const bodyParser = require("body-parser");
// create an instance of express to serve our end points
const app = express();
// we'll load up node's built in file system helper library here
// (we'll be using this later to serve our JSON files
const fs = require("fs");
// configure our express instance with some body-parser settings
// including handling JSON data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// this is where we'll handle our various routes from
const routes = require("./routes/routes.js")(app, fs);
// finally, launch our server on port 5000.
const server = app.listen(5000, () => {
console.log("listening on port %s...", server.address().port);
});
控制台错误 Output:
Network request failed
- node_modules\whatwg-fetch\dist\fetch.umd.js:511:17 in setTimeout$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:135:14 in _callTimer
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:387:16 in callTimers
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:425:19 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112:6 in __guard$argument_0- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:373:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:4 in callFunctionReturnFlushedQueue
* [native code]:null in callFunctionReturnFlushedQueue
更新了服务器以使用 HTTPS:
// load up the express framework and body-parser helper
const express = require("express");
const bodyParser = require("body-parser");
// create an instance of express to serve our end points
const app = express();
// we'll load up node's built in file system helper library here
// (we'll be using this later to serve our JSON files
const fs = require("fs");
const https = require('https');
// configure our express instance with some body-parser settings
// including handling JSON data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// this is where we'll handle our various routes from
const routes = require("./routes/routes.js")(app, fs);
// finally, launch our server on port 5000.
const server = https.createServer({
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.cert')
}, app).listen(5000, () => {
console.log("listening on port %s...", server.address().port);
});
HTTPS 更新后的控制台错误 Output:
Network request failed
- node_modules\whatwg-fetch\dist\fetch.umd.js:505:17 in setTimeout$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:135:14 in _callTimer
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:387:16 in callTimers
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:425:19 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112:6 in __guard$argument_0- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:373:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:4 in callFunctionReturnFlushedQueue
* [native code]:null in callFunctionReturnFlushedQueue
HTTPS 服务器的 Postman 中的测试结果: GET
请求有效,但是我必须设置 Postman 以接受自签名证书。
我运行expo eject
以公开整个Info.plist
NSAppTransportSecurity
的密钥和字典已经在Info.plist
设置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
<string>csharp_frontend</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>xxx.xxx.xxx.x</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
<key>NSLocationWhenInUseUsageDescription</key>
<string/>
<key>UILaunchStoryboardName</key>
<string>SplashScreen</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleDefault</string>
<key>UIRequiresFullScreen</key>
<true/>
</dict>
</plist>
更新了 Info.plist 以包含服务器的 IP 地址而不是 localhost
<dict>
<key>xxx.xxx.xxx.x</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
错误略有改变 - 新错误 - '373:10 in __guard'
Network request failed
- node_modules\whatwg-fetch\dist\fetch.umd.js:505:17 in setTimeout$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:135:14 in _callTimer
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:387:16 in callTimers
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:425:19 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112:6 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:373:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:4 in callFunctionReturnFlushedQueue
* [native code]:null in callFunctionReturnFlushedQueue
也许问题:我认为由于 SSL 证书是自签名的,因此反应原生前端将请求视为 HTTP 而不是 HTTPS。
有任何想法吗??
如果您在 Android 平台上运行并使用 API 级别 > 27,那么我认为这个问题与 http 协议限制有关如果是这样,您需要将android:usesCleartextTraffic="true"添加到 AndroidManifest.xml 以允许 http 流量:
<?xml version="1.0" encoding="utf-8"?>
<manifest...>
<application
...
android:usesCleartextTraffic="true"
...>
</application>
</manifest>
如果 iOS 版本 >= 8
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
...
</dict>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.