Node.js 连接react本机和节点后端(restify服务器)时出现网络错误

Node.js 连接react本机和节点后端(restify服务器)时出现网络错误,node.js,reactjs,react-native,axios,restify,Node.js,Reactjs,React Native,Axios,Restify,通过Axios将react本机应用程序连接到节点后端服务器时出现网络错误。 react native和restify服务器代码都附在下面。 当你想让邮递员进来的时候,没关系。然而,当我们使用Axios获取数据时,它显示了一个网络错误 import React from 'react'; import { StyleSheet, Text, View, Button } from 'react-native'; import axios from 'axios'; class ViewPatien

通过Axios将react本机应用程序连接到节点后端服务器时出现网络错误。 react native和restify服务器代码都附在下面。 当你想让邮递员进来的时候,没关系。然而,当我们使用Axios获取数据时,它显示了一个网络错误

import React from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import axios from 'axios';
class ViewPatients extends React.Component {
  constructor(props){
    super(props);  
  axios.get('http://127.0.0.1:3006/patients', {
   
  })
  .then(function (response) {
    alert(JSON.stringify(response.data));
  })
  .catch(function (error) {
    console.log(error.message)
    alert(error.message);
  });
}

  render() {
    return (
      <View style={styles.container}>
        <Text>Add friends here!</Text>

        <Button
          title="Back to home"
          onPress={() =>
            this.props.navigation.navigate('Home')
          }
        />
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});
export default ViewPatients;

从“React”导入React;
从“react native”导入{样式表、文本、视图、按钮};
从“axios”导入axios;
类ViewReact.Component{
建造师(道具){
超级(道具);
axios.get()http://127.0.0.1:3006/patients', {
})
.然后(功能(响应){
警报(JSON.stringify(response.data));
})
.catch(函数(错误){
console.log(错误消息)
警报(错误消息);
});
}
render(){
返回(
在这里添加朋友!
this.props.navigation.navigate('Home'))
}
/>
);
}
}
const styles=StyleSheet.create({
容器:{
弹性:1,
背景颜色:“#fff”,
对齐项目:“居中”,
为内容辩护:“中心”,
},
});
导出默认视图;
服务器代码


'''
var SERVER_NAME = 'Sravyas-api'
var PORT = 3006;
var HOST = '127.0.0.1';
var countGetReq = 0;
var countPostReq = 0;

//setup cors
// restify.CORS.ALLOW_HEADERS.push('accept');
// restify.CORS.ALLOW_HEADERS.push('sid');
// restify.CORS.ALLOW_HEADERS.push('lang');
// restify.CORS.ALLOW_HEADERS.push('origin');
// restify.CORS.ALLOW_HEADERS.push('withcredentials');
// restify.CORS.ALLOW_HEADERS.push('x-requested-with');
// server.use(restify.CORS());



var restify = require('restify')

  // Get a persistence engine for the users
  , patientSave = require('save')('products')

  // Create the restify server
  , server = restify.createServer({ name: SERVER_NAME})

  server.use(
    function crossOrigin(req,res,next){
      res.header('Access-Control-Allow-Credentials', true);
      res.header("Access-Control-Allow-Origin", "*");
      res.header("Access-Control-Allow-Headers", "X-Requested-With");
      return next();
    }
  );
  server.listen(PORT, HOST, function () {
    console.log("=================================================");
  console.log('Server %s listening at %s', server.name, server.url)
  console.log("EndPoints:");
  console.log(server.url+"/patients    :>"+"method:GET,POST");
  console.log("=================================================");
  console.log('Resources:')
  console.log(' /products')
  console.log(' /products/:id')  
  console.log("=================================================");
})

server
  // Allow the use of POST
  .use(restify.fullResponse())

  // Maps req.body to req.params so there is no switching between them
  .use(restify.bodyParser())

// Get all products in the system
server.get('/patients', function (req, res, next) {

  patientSave.find({}, function (error, product) {
    
  console.log("----------------------->>>>>>>>products GET:request received<<<<<<<<<<<<--------------------------")
    countGetReq++;
console.log(product);
    res.send(product)
    console.log("-------------------------->>>>>>>>>>>>>>>>>>>products GET:request response<<<<<<<<<<<<-------------------------")
  })
console.log("processed Request Count==>GET " +countGetReq +"POST"+countPostReq);
})

// Get a single product by their product id
server.get('/patients/:id', function (req, res, next) {


  patientSave.findOne({ _id: req.params.id }, function (error, product) {

    if (error) return next(new restify.InvalidArgumentError(JSON.stringify(error.errors)))

    if (product) {
     
      res.send(product)
    } else {
    
      res.send(404)
    }
  })
})

// Create a new product
server.post('/patients', function (req, res, next) {

  
  
  var newPatient = {
        id: req.params.id, 
name: req.params.name,
    address: req.params.address,
    dateOfBirth: req.params.dateOfBirth,
    department: req.params.department,
    doctor: req.params.doctor

    }


  patientSave.create( newPatient, function (error, product) {

    countPostReq++;
    if (error) return next(new restify.InvalidArgumentError(JSON.stringify(error.errors)))
    console.log("processed Request Count==>GET " +countGetReq +"POST"+countPostReq);
  
    res.send(201, product)
  })
})




'''
var SERVER_NAME='Sravyas api'
var端口=3006;
var-HOST='127.0.0.1';
var countGetReq=0;
var countPostReq=0;
//设置cors
//restify.CORS.ALLOW_HEADERS.push('accept');
//restify.CORS.ALLOW_HEADERS.push('sid');
//restify.CORS.ALLOW_HEADERS.push('lang');
//restify.CORS.ALLOW_HEADERS.push('origin');
//restify.CORS.ALLOW_HEADERS.push('withcredentials');
//resify.CORS.ALLOW_HEADERS.push('x-request-with');
//use(restify.CORS());
var restify=require('restify')
//为用户获取持久性引擎
,patientSave=require('save')('products'))
//创建restify服务器
,server=restify.createServer({name:server\u name})
server.use(
函数交叉原点(req、res、next){
res.header('Access-Control-Allow-Credentials',true);
res.header(“访问控制允许原点”、“*”);
res.header(“访问控制允许标头”、“X请求的标头”);
返回next();
}
);
侦听(端口、主机、函数(){
console.log(“===============================================================================”);
console.log('服务器%s在%s上侦听',Server.name,Server.url)
log(“端点:”);
log(server.url+“/patients:>”+“方法:GET,POST”);
console.log(“===============================================================================”);
console.log('Resources:')
console.log(“/products”)
console.log(“/products/:id”)
console.log(“===============================================================================”);
})
服务器
//允许使用POST
.use(restify.fullResponse())
//将req.body映射到req.params,因此它们之间没有切换
.use(restify.bodyParser())
//获取系统中的所有产品
获取('/patients',函数(req、res、next){
patientSave.find({},函数(错误,产品){

console.log(“-------------------------->>>>>>>>>>>产品获取:收到请求>>>>>>>>>>产品获取:请求响应问题:Axios在不同的主机上吗?实际错误是什么?“网络错误”可能意味着许多不同的事情。