express - 将Android模拟器上的本机反应连接到表达服务器
问题描述
我刚刚开始使用原生反应,我可以使用 Expo XDE 运行它。前端工作正常,但是当客户端(模拟器)向服务器(nodejs-express)发送“GET”请求时出现问题。
出现网络错误,如何将模拟器连接到express服务器?我使用了localhost ip,这是正确的方法吗?错误是关于我的手机和我的电脑之间的连接。我使用了localhost ip,但是当我用计算机本地ip替换它时,它就可以了。此外,如果您使用 android studio,您可以使用 10.0.0.2 或 10.0.0.3 进行 genymotion。
客户
import React from "react";
import { StyleSheet, Text, View, Button } from "react-native";
import axios from "axios";
export default class App extends React.Component {
fetchData() {
alert("clicked");
axios
.get("http://localhost:3000")
.then(function(res) {
console.log("success");
})
.catch(function(err) {
console.log(err);
});
}
render() {
return (
<View style={styles.container}>
<Text>Open up App.js to start working on your app!</Text>
<Button
onPress={this.fetchData}
title="Learn More"
color="#841584"
Label="Learn more"
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center"
}
});
服务器
const express=require('express');
const bodyParser=require('body-parser')
var Port=process.env.PORT||3000;
const app=express();
app.use(bodyParser.json);
app.use(bodyParser.urlencoded({extended:false}));
app.listen(Port,()=>console.log(`server started at 3000`));
app.get('/',(req,res)=>{
//res.send({data:"data"})
console.log(console.log("request is recieved"))
})
解决方案
推荐阅读
- continuous-integration - Drone CI/CD 只停留在 exec 管道中
- postgresql - 带有 PostgreSQL 微服务(带分页)和 Cassandra 微服务(不带分页)的 JHipster 微服务应用程序
- r - 无法将一个数据集隔离到另一个数据集的子集中
- c++ - 使用 C++ 在 Visual Studio 19 中读取二进制文件时出错
- tmux - 是否有可能从 tmux 开始?
- php - ajax成功后jquery .css函数不起作用
- android-emulator - 为什么我不断收到 Database Inspector 警告?
- python - 如何将分段函数应用于 2D numpy 数组的每个元素
- firebase - 如何将 Firebase 动态链接中的参数用作 Flutter 命名路由参数
- java - Android 工具链状态未知