【问题标题】:how to debug apis in nodejs如何在nodejs中调试api
【发布时间】:2018-11-12 11:33:46
【问题描述】:

我在 nodejs 中从邮递员发送了一个请求,作为 nodejs 的初学者,我发现很难调试。有人可以对此有所了解吗? 这是此时的整个存储库。 https://github.com/kolaveridi/socialnetwork

router.get('/handle/:handle', (req, res) => {
  console.log('working');
  console.log(req.params.handle);
  const errors = {};
  Profile.findOne({ handle: req.params.handle })
    .populate('user', ['name', 'avatar'])
    .then(profile => {
      if (!profile) {
        errors.noprofile = 'There is no profile for this user';
        console.log('no profile');
        res.status(404).json(errors);
      }

      res.json(profile);
    })
    .catch(err => res.status(404).json(err));
});

我没有看到任何控制台正在发送此请求 当我已经有一个用户的句柄“妈妈”注册,登录。 http://localhost:5000/api/profile/handle/:mom

【问题讨论】:

  • 起初,您的问题标题看起来非常笼统和糟糕。请更改它。遇到问题,我可以知道你得到的错误响应吗?我已经克隆了你的 repo 并在我的本地执行。我可以访问你的 api 并查看控制台日志。
  • @Sreehari 你能解释一下导致错误的原因吗?为什么我不能通过他的句柄访问用户。
  • 为此,您需要向我提供错误。为您返回的错误是什么?你是如何测试它的?

标签: node.js api


【解决方案1】:

问题不在于源代码本身,而在于您用于测试应用程序的 api url。 ":" 只需要在 "handle" 路由声明中,但在 url 中你需要跳过它:

http://localhost:5000/api/profile/handle/mom

关于问题:您可以使用console.log 并内置调试器(node inspect server.js),也可以使用IDE 调试器。

【讨论】:

  • 嘿,我这样做了,但它仍然未能提供有关用户的任何数据。你能克隆并帮助我吗?
猜你喜欢
  • 2020-10-20
  • 2020-03-08
  • 2013-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多