kuwoMusicApi icon indicating copy to clipboard operation
kuwoMusicApi copied to clipboard

请求接口的时候会随机崩溃

Open AffectionParadise opened this issue 3 years ago • 6 comments

每次请求接口都有一定概率崩溃,信息如下

nodejs.JSONResponseFormatError: Unexpected token E in JSON at position 0 (data json format: "Error occured while trying to proxy to: www.kuwo.cn/api/www/bang/bang/musicList?bangId=12&pn=1&rn=50&httpsStatus=1&reqId=cd177cbe-88de-42ca-9137-24060680b175"), GET http://www.kuwo.cn/api/www/bang/bang/musicList?bangId=12&pn=1&rn=50&httpsStatus=1&reqId=cd177cbe-88de-42ca-9137-24060680b175 504 (connected: true, keepalive socket: false, agent status: {"createSocketCount":249,"createSocketErrorCount":0,"closeSocketCount":248,"errorSocketCount":0,"timeoutSocketCount":247,"requestCount":401,"freeSockets":{},"sockets":{"www.kuwo.cn:80:":1},"requests":{}}, socketHandledRequests: 1, socketHandledResponses: 1) headers: {"server":"nginx","date":"Sun, 27 Feb 2022 14:38:35 GMT","content-type":"application/json; charset=utf-8","content-length":"157","connection":"keep-alive","set-cookie":["kw_token=SVJFKJ3RR68; path=/; expires=Tue, 29 Mar 2022 14:38:34 GMT"],"vary":"Accept-Encoding"} headers: {"server":"nginx","date":"Sun, 27 Feb 2022 14:38:35 GMT","content-type":"application/json; charset=utf-8","content-length":"157","connection":"keep-alive","set-cookie":["kw_token=SVJFKJ3RR68; path=/; expires=Tue, 29 Mar 2022 14:38:34 GMT"],"vary":"Accept-Encoding"} at JSON.parse () at parseJSON (C:\Users\30710\Desktop\kuwoMusicApi\node_modules\urllib\lib\urllib.js:1227:24) at C:\Users\30710\Desktop\kuwoMusicApi\node_modules\urllib\lib\urllib.js:932:23 at decodeContent (C:\Users\30710\Desktop\kuwoMusicApi\node_modules\urllib\lib\urllib.js:733:14) at handleResponseCloseAndEnd (C:\Users\30710\Desktop\kuwoMusicApi\node_modules\urllib\lib\urllib.js:913:7) at IncomingMessage. (C:\Users\30710\Desktop\kuwoMusicApi\node_modules\urllib\lib\urllib.js:957:7) at IncomingMessage.emit (node:events:402:35) at endReadableNT (node:internal/streams/readable:1343:12) at processTicksAndRejections (node:internal/process/task_queues:83:21) name: "JSONResponseFormatError" data: "Error occured while trying to proxy to: www.kuwo.cn/api/www/bang/bang/musicList?bangId=12&pn=1&rn=50&httpsStatus=1&reqId=cd177cbe-88de-42ca-9137-24060680b175" path: "/api/www/bang/bang/musicList?bangId=12&pn=1&rn=50&httpsStatus=1&reqId=cd177cbe-88de-42ca-9137-24060680b175" status: 504 headers: {"server":"nginx","date":"Sun, 27 Feb 2022 14:38:35 GMT","content-type":"application/json; charset=utf-8","content-length":"157","connection":"keep-alive","set-cookie":["kw_token=SVJFKJ3RR68; path=/; expires=Tue, 29 Mar 2022 14:38:34 GMT"],"vary":"Accept-Encoding"} res: {"status":504,"statusCode":504,"statusMessage":"Gateway Timeout","headers":{"server":"nginx","date":"Sun, 27 Feb 2022 14:38:35 GMT","content-type":"application/json; charset=utf-8","content-length":"157","connection":"keep-alive","set-cookie":["kw_token=SVJFKJ3RR68; path=/; expires=Tue, 29 Mar 2022 14:38:34 GMT"],"vary":"Accept-Encoding"},"size":157,"aborted":false,"rt":1114,"keepAliveSocket":false,"data":"Error occured while trying to proxy to: www.kuwo.cn/api/www/bang/bang/musicList?bangId=12&pn=1&rn=50&httpsStatus=1&reqId=cd177cbe-88de-42ca-9137-24060680b175","requestUrls":["http://www.kuwo.cn/api/www/bang/bang/musicList?bangId=12&pn=1&rn=50&httpsStatus=1&reqId=cd177cbe-88de-42ca-9137-24060680b175"],"timing":null,"remoteAddress":"39.155.179.10","remotePort":80,"socketHandledRequests":1,"socketHandledResponses":1} pid: 11344

AffectionParadise avatar Feb 27 '22 14:02 AffectionParadise

应该是访问过快返回504了,这时候应该等约1秒再访问一次,访问个3~4次就成功了。

wifi-left avatar Jan 18 '23 02:01 wifi-left

这玩意,酷我官网都是这样,不清楚是咋回事,刷着刷着接口就挂了

---原始邮件--- 发件人: @.> 发送时间: 2023年1月18日(周三) 上午10:56 收件人: @.>; 抄送: @.***>; 主题: Re: [QiuYaohong/kuwoMusicApi] 请求接口的时候会随机崩溃 (Issue #6)

应该是访问过快返回504了,这时候应该等约1秒再访问一次,访问个3~4次就成功了。

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

qyhqiu avatar Jan 18 '23 02:01 qyhqiu

加个 try catch包在外面可能就可以了(指仓库代码)。

wifi-left avatar Jan 18 '23 02:01 wifi-left

这玩意,酷我官网都是这样,不清楚是咋回事,刷着刷着接口就挂了 ---原始邮件--- 发件人: @.> 发送时间: 2023年1月18日(周三) 上午10:56 收件人: @.>; 抄送: @.>; 主题: Re: [QiuYaohong/kuwoMusicApi] 请求接口的时候会随机崩溃 (Issue #6) 应该是访问过快返回504了,这时候应该等约1秒再访问一次,访问个3~4次就成功了。 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.>

这是官方的保护机制。有概率访问504,我一般都是返回504后重新发送一次fetch请求。

wifi-left avatar Jan 18 '23 02:01 wifi-left

这样的嘛,了解了居然还有这种扯淡的保护机制,我看看哪天重新整一下

---原始邮件--- 发件人: @.> 发送时间: 2023年1月18日(周三) 中午11:00 收件人: @.>; 抄送: @.@.>; 主题: Re: [QiuYaohong/kuwoMusicApi] 请求接口的时候会随机崩溃 (Issue #6)

这玩意,酷我官网都是这样,不清楚是咋回事,刷着刷着接口就挂了 … ---原始邮件--- 发件人: @.> 发送时间: 2023年1月18日(周三) 上午10:56 收件人: @.>; 抄送: @.>; 主题: Re: [QiuYaohong/kuwoMusicApi] 请求接口的时候会随机崩溃 (Issue #6) 应该是访问过快返回504了,这时候应该等约1秒再访问一次,访问个3~4次就成功了。 — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.>

这是官方的保护机制。有概率访问504,我一般都是返回504后重新发送一次fetch请求。

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

qyhqiu avatar Jan 18 '23 03:01 qyhqiu

这是我用 request 实现的一个请求方法,您可以参考下。 URL:访问网址、Header:请求头 Refunc:返回的函数,返回信息后将会调用这个函数 Otherc:在调用返回函数额外的参数(除了data外)

function fetchinfo(url, headers, recfunc, otherc) {
    const options = {
        url: url,
        headers: headers
    };

    function callback(error, response, body) {

        if (!error && response.statusCode == 200) {
            try {
                recfunc(JSON.parse(body), otherc);
                return;
            } catch (e) {

            }
            recfunc(body, otherc);
            return;
            // const info = JSON.parse(body);
            // console.log(info.stargazers_count + " Stars");
            // console.log(info.forks_count + " Forks");
        } else {
            recfunc(body, otherc);
        }
    }

    request(options, callback);
});

我这边是直接将错误返回给前端,让前端来处理。 避免后端处理错误直接崩溃。

wifi-left avatar Jan 18 '23 03:01 wifi-left