首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Expressjs服务器无法处理来自外部的请求

Expressjs服务器无法处理来自外部的请求
EN

Stack Overflow用户
提问于 2022-06-02 09:13:38
回答 2查看 539关注 0票数 1

我有一个包含React组件的ExpressJs服务器。服务器应该处理来自外部的请求,当当前不播放时,一个请求应该播放Spotify API中的歌曲。

代码语言:javascript
运行
复制
app.post("/play", (req, res) => {
  try {
    // requesting to play uses query params
    id = req.query.id;
    currPlayingID = 0;

    // get the currently playing song from the SPotify API
    axios({
      url: "https://api.spotify.com/v1/me/player/currently-playing",
      method: "get",
      headers: {
        authorization: `Bearer ${access_token}`,
      },
    })
      // set the currently Playing ID or to zero if nothing is playing
      .then((response) => {
        if (response.data !== null) {
          currPlayingID = response.data.id;
        } else {
          currPlayingID = 0;
        }
      });

    // only play the song if its not currently playing
    if (id !== currPlayingID) {
      // making a axios request to the Spotify API to play the Song with the ID
      axios({
        url: "https://api.spotify.com/v1/me/player/play/",
        method: "put",
        headers: {
          authorization: `Bearer ${access_token}`,
        },
        data: {
          uris: [`spotify:track:${id}`],
        },
      });
      res.status(204);
    }
  } catch (error) {
    res
      .status(404)
      .json({ message: "Couldn't get Info from Spotify API", error: error });
  }
});

问题是:

当我在设备本身上启动服务器(因此在我的桌面PC上启动本地服务器)时,代码可以工作,但是当我在RaspberryPI上启动服务器时,我无法处理对这个端点/play的请求。是的,我更新了所有的IP地址,到处都是。

但是更有趣的部分是使用React客户端,我得到了这个错误:

代码语言:javascript
运行
复制
Failed to load resource: net::ERR_CONNECTION_REFUSED

向邮递员提出请求,我得到以下信息:

代码语言:javascript
运行
复制
Mixed Content Error: The request has been blocked because it requested an insecure HTTP resource

从使用python脚本的请求中,我可以在服务器端获得:

代码语言:javascript
运行
复制
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "AxiosError: Request failed with status code 400".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

我不知道如何修复每一个错误,如果它是一个修复。基本上,我发现拒绝来自外部本地主机的请求是一个问题,因为在我的ssh终端上使用cURL可以工作。

EN

回答 2

Stack Overflow用户

发布于 2022-06-02 15:34:06

我在学快递,所以我不是专家,但我在看你的错误。我建议你试试asyncHandler模块。它处理异步请求和异常。

票数 0
EN

Stack Overflow用户

发布于 2022-07-30 05:13:13

我遇到了类似的问题,因为当我通过Axios发送API请求时,我的令牌为空/空/错,所以要确保令牌是正确的

这是我的请求格式

代码语言:javascript
运行
复制
        axios({
               method:"POST",
               url:"https://graph.facebook.com/v13.0/"+phon_no_id+"/message?access_token="+token,
               data:{
                   messaging_product:"whatsapp",
                   to:from,
                   text:{
                       body:"Hi.. I'm Prasath"
                   }
               },
               headers:{
                   "Content-Type":"application/json"
               }

           });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72473690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档