【问题标题】:NodeJs not running in the port 4000NodeJs 不在端口 4000 中运行
【发布时间】:2019-10-22 23:15:08
【问题描述】:

我正在尝试在 react 中使用 axios 调用 api。我正在使用 express 和 node js。当使用 axios.get() 调用 api 时。它在一段时间后返回错误。当我在端口 4000 中运行节点时(localhost :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');
  });

【问题讨论】:

  • 你到底得到了什么错误信息?
  • 错误情况下你没有响应请求...
  • 它等待响应并返回此错误' GET localhost:4000/issue net::ERR_EMPTY_RESPONSE '

标签: node.js reactjs mongodb express axios


【解决方案1】:

您需要处理 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);
    });
  });

【讨论】:

  • 感谢您的回复。我尝试了您的建议。但它什么也没做。即使我在标签中运行localhost:4000/issue 它也不起作用。
  • 您有超过 1 个问题吗?也许你的请求不好Issue.find().then((err, issue) => {
  • 很高兴它有帮助!继续破解!
猜你喜欢
  • 2019-09-13
  • 2020-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-19
  • 2021-12-16
  • 1970-01-01
相关资源
最近更新 更多