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



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

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上启动服务器时,我无法处理对此端点/播放的请求。是的,我更新了所有的IP地址,到处都是。

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

Failed to load resource: net::ERR_CONNECTION_REFUSED

请求与邮差我得到以下:

Mixed Content Error: The request has been blocked because it requested an insecure HTTP resource

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

[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'
}

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

我正在学习表达,所以我不是专家,但我在看你的错误。我建议您尝试asyncHandler模块。处理异步请求和异常。

我遇到了一个类似的问题,因为当我通过Axios,我的令牌是空/空/错误的,所以确保你的令牌是正确的

这是我的请求格式

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"
}
});

最新更新