简体   繁体   English

如何调试对Node.js Express应用程序的POST请求?

[英]How to debug a POST request to a Node.js Express application?

I'm new to nodejs and I'm migrating my current API from python to nodejs using express. 我是nodejs的新手,我正在使用express将我当前的API从python迁移到nodejs。

What I'm trying to do is to make a request to an external API. 我正在尝试做的是向外部API发出请求。 I'm pretty sure my API call is right, since I copied from the external API example: 我很确定我的API调用是正确的,因为我从外部API示例复制了:

exports.getBalance = function() {
  return new Promise(function(resolve, reject) {
    var command_url = "/v1/transaction/getBalance";
    var full_url = API_URL + command_url;
    var nonce = getNonce();

    var data = "username=" + convertUsername(API_USER) + "&nonce=" + nonce;

    const signature = makeSignature(nonce, data, command_url);

    var form = {
      username: API_USER,
      nonce: nonce
    };

    var formData = querystring.stringify(form);
    var contentLength = formData.length;

    var headers = {
      "X-API-KEY": API_KEY,
      "X-API-SIGN": signature,
      "X-API-NONCE": nonce,
      "Content-Length": contentLength,
      "Content-Type": "application/x-www-form-urlencoded"
    };

    request(
      {
        url: full_url,
        method: "POST",
        headers: headers,
        body: formData
      },
      function(error, response, body) {
        if (!error) {
          body = JSON.parse(body);
          if (response.statusCode == 200) {
            resolve(body);
          } else {
            reject(error);
          }
        } else {
          console.log("error:", error);
          reject(error);
        }
      }
    );
  });

This is my express route: 这是我的快递路线:

routes.post("/balance", mistertango.getBalance);

However, when I try to POST to this route, I don't receive nothing. 但是,当我尝试POST到这条路线时,我什么都没收到。 I use Insomnia to run API tests, so Insomnia keeps running with no response from my express API. 我使用Insomnia来运行API测试,因此Insomnia一直运行而没有来自我的快速API的响应。

I'd like to know how can I debug my code? 我想知道如何调试我的代码? I'd like to make an API call using Insomnia to my express API, and check if I'm getting a response from my external API request. 我想使用Insomnia对我的快速API进行API调用,并检查我是否从我的外部API请求获得响应。

Thanks 谢谢

mistertango.getBlance returns a Promise but express doesn't handle promises by default. mistertango.getBlance返回一个Promise但express默认不处理promises。 You need to call res.send(data) to actually send a response to the client. 您需要调用res.send(data)来实际向客户端发送响应。

routes.post("/balance", async (req, res, next) => {
  try {
    const balance = await mistertango.getBalance()
    res.send({ balance })
  } catch (error) {
    next(error)
  }
})

Or without async/await: 或者没有异步/等待:

routes.post("/balance", (req, res, next) => {
  mistertango.getBalance()
    .then(balance => res.send({ balance }))
    .catch(error => next(error))
})

Note 1 : You might be able to use res.send(balance) instead of res.send({ balance }) as long as balance is not a number. 注1 :只要balance不是数字,您就可以使用res.send(balance)而不是res.send({ balance }) (Response body cannot be a raw number, so I've wrapped it in an object). (响应主体不能是原始数字,所以我将它包装在一个对象中)。

Note 2 : In both cases, we have to use .catch or try/catch to handle any errors because express won't handle rejected promises on its own. 注2 :在这两种情况下,我们都必须使用.catch或try / catch来处理任何错误,因为express不会自己处理被拒绝的promise。 You can use express-promise-router to fix that! 您可以使用express-promise-router来解决这个问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM