NodeJ未在端口4000中运行

时间:2019-06-07 12:34:44

标签: node.js reactjs mongodb express axios

我试图在反应中使用axios调用api。我使用express和node js。当使用axios.get()调用api时,一段时间后返回错误。当我在端口4000(localhost)中运行node时:4000 / data)无法加载。

  //api
  router.route('/issue').get((req, result) => {    
  Issue.find((err, issue) => {
    if (err)
        console.log(err);
    else
        result.json(issue);
    });
  });

  //api call in react file
  axios.get('http://localhost:4000/issue').then(res=>{
        console.log('success');
    }).catch(err=>{
        console.log('error');
  });

1 个答案:

答案 0 :(得分:0)

您需要处理api中的错误。如果您只是console.log,则您的前端仍在等待响应

如果您的后背没有发送任何响应,则您的浏览器会因超时而取消请求,这就是您收到的错误

 //api
  router.route('/issue').get((req, result) => {    
  Issue.find((err, issue) => {
    if (err)
        result.status(404).json({
            success: false,
            msg: "There has been a problem in your request"
          });
    else
        result.json(issue);
    });
  });