node.js - 我无法向服务器发送数据,也不能,也没有从服务器接收数据

标签 node.js react-native

我正在学习react-native,开发登录功能时遇到一些问题

这是我在react-native中的获取函数

getLogin = () => {
fetch("http://localhost:5000/auth/login", {
  method: 'POST',
  header: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    email: `${this.state.email}`,
    pass: `${this.state.pass}`,
  }),
})
  .then((res) => res.json())
  .then((data) => {
    console.log(this.state)
    console.log(data)
    if (!this.state.success) {
      console.log(data)
      console.log("Login false!");
    }
    else {
      console.log(data);
      console.log("Login success");
    }
  })
  .catch((error) => {
    console.error(error);
  });
 }

这是我在服务器中的代码

module.exports.postLogin = (req, res) => {
  console.log(req)
  var email = req.body.email
  var pass = req.body.pass
  var user = db.get('users').find({ email: email }).value()

  if (!user) {
  res.json({
   errors: [
     "user does not exist "
    ],
     values: req.body,
   })
 }

 if (user.pass !== pass) {
   res.json({
   errors: [
       "wrong pass "
    ],
    values: req.body,
  })
}

 res.json({
   success: true
 })
 res.cookie("userID", user.id, {
   signed: true
 })  
}

我无法向服务器发送数据,我也不能,也没有从服务器接收数据 请帮助我,谢谢你的sp

最佳答案

您有两种解决方案:

  1. 使用您计算机的IP 地址而不是本地主机,并确保您的计算机以及您的设备和模拟器在同一网络中运行

  2. 解决方案2:您可以使用ngrok https://ngrok.com/也就是说,将为在端口:5000 运行的服务器代码生成一个 HTTPS URL,并且可以从其他设备访问它

关于node.js - 我无法向服务器发送数据,也不能,也没有从服务器接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58743059/

相关文章:

node.js - Google API dailyLimit超出解决方案

node.js - NodeJS、RSA,这安全吗?

node.js - 生产环境中的 NodeJS 和 Socket.io : Dealing with state

javascript - 在javascript中用 "!"(感叹号)为数组中的所有字符串添加前缀的最简单方法是什么?

javascript - 我如何在 React Native 中使用 map 来创建单独的按钮?

android - React Native - 位置 "absolute"在 Android 中不工作

node.js - 使用 Node 和 Redis 获取 10 个随机项目

ios - 图像未出现在 React Native 应用程序的 ListView 中

android - 到目前为止,React Native 与 WPA 应用程序的体验

react-native - React Native [网络错误] : TypeError: Network request failed using Apollo Client