简体   繁体   English

Azure throws error when making an axios http request in node.js API

[英]Azure throws error when making an axios http request in node.js API

I created a node application using express and uploaded it to an Azure web application.我使用 express 创建了一个节点应用程序并将其上传到 Azure web 应用程序。 Whenever I send a response like每当我发送类似的回复时

router.get("/search=:search", (req, res) => res.send("Response"));

The API works fine and I get the response, but I included axios to make http requests such as: API 工作正常,我得到了响应,但我包含了 axios 以发出 http 请求,例如:

const http = require("axios");

const url = "https://yts.torrentbay.to/api/v2/list_movies.jsonp?query_term=shrek";

router.get("/search=:search", async (req, res) => {
  await http.get(url);
  res.send("Response")
});

Now this works perfectly on my local machine when testing but Azure throws a 500 Internal Server Error .现在这在测试时在我的本地机器上完美运行,但 Azure 抛出500 Internal Server Error Should I use the built-in http library in node?我应该在节点中使用内置的 http 库吗? Could the problem be where the request is made to the URL?问题可能出在向 URL 发出请求的位置吗?

EDIT: I discovered that the problem is with the URL.编辑:我发现问题出在 URL 上。 I tried google.com and it worked so how could I fix this?我尝试google.com并且它有效,那么我该如何解决这个问题?

  • if you want the api to work with the wrong url then one of the work around is to add a catch block(error handling )如果您希望 api 与错误的 url 一起工作,那么解决方法之一是添加一个 catch 块(错误处理)

  • you are getting this error because the http.get is not getting any response and it is waiting for a long time.您收到此错误是因为http.get没有得到任何响应并且等待了很长时间。

  • You can add a catch block which will get triggered when the http.get fails to get a response.您可以添加一个 catch 块,当http.get无法获得响应时触发该块。

  • Inside the catch you can then add a response thus even if you don't get response in http.get you get a response from your own Api.然后,即使您在http.get中没有得到响应,您也可以在 catch 中添加响应。得到您自己的 Api 的响应。


router.get("/search=:search", async (req, res) => {
    await  http.get(url).catch((err)=>{
                                          res.send("error has happend ");
                                      });
    res.send("Response");
});

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

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