Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 发送多个请求挂起在节点中_Node.js_Heroku_Request - Fatal编程技术网

Node.js 发送多个请求挂起在节点中

Node.js 发送多个请求挂起在节点中,node.js,heroku,request,Node.js,Heroku,Request,我正在连接到Riot API,以便使用请求包从他们的data dragon服务获取图像。一旦请求给我一个响应,我就将映像保存到磁盘上。保存冠军缩略图是轻而易举的事;没问题。然而,项目缩略图在95%的情况下失败。它只是偶尔在我本地的机器上工作 我已经将该应用程序部署到Heroku,在我部署的4/4次中,图像都已成功加载。不幸的是,如前所述,当我在本地运行服务器时,我的internet实际上会挂起,只有在尝试获取项目缩略图时,我才能使用internet 以下是一个片段: function retri

我正在连接到Riot API,以便使用请求包从他们的data dragon服务获取图像。一旦请求给我一个响应,我就将映像保存到磁盘上。保存冠军缩略图是轻而易举的事;没问题。然而,项目缩略图在95%的情况下失败。它只是偶尔在我本地的机器上工作

我已经将该应用程序部署到Heroku,在我部署的4/4次中,图像都已成功加载。不幸的是,如前所述,当我在本地运行服务器时,我的internet实际上会挂起,只有在尝试获取项目缩略图时,我才能使用internet

以下是一个片段:

function retrieveAndProcessImage(imageURL, callback) {
    "use strict";

    console.log(imageURL);

    request(imageURL, {encoding: 'binary'}, function (req_err, req_res) {

        // Extract the image name by looking at the very end of the path.
        var pathArray = (imageURL).split('/');
        var imageName = pathArray[pathArray.length - 1];

        if (req_err) {
            console.log("Couldn't retrieve " + imageName);
        } else {
            callback(imageName, req_res['body']);
        }
    });
};
上面的代码片段从API中获取图像。当它偶尔工作时,正确的图像会被保存,这样我至少知道URL都是正确的。回调只是一个fs.writeFile,它在本地写入文件,因此我不必再发送另一个请求

function retrieveAndProcessJson(dataURL, callback) {
    "use strict";

    request(dataURL, function (req_err, req_res) {
        // Try to parse the JSON.
        try {
            var bodyJSON = JSON.parse(req_res['body']);
        } catch (err) {
            _errorMessage(dataURL);
            return;
        }

        // If the parsing failed or the retrieval failed, connection failed.
        if (bodyJSON['status'] !== undefined) {
            _errorMessage(dataURL);
            return;
        }

        // Otherwise output the success.
        console.log("Connected to " + dataURL);

        // Callback.
        callback(req_res['body']);
    });
};
上面的代码获取原始JSON并将其结果传递给回调

fileFuncs.retrieveAndProcessJson(dataURL, function (raw_body) {
    var bodyJSON = JSON.parse(raw_body);
    _saveData(raw_body);
    _saveImages(bodyJSON['data'])
});
保存图像的方法是:

function _saveImages(dataJSON) {
    var filePath;
    var itemKey;
    var item;

    var imageURL;
    var imageName;

    // Check to see the destination folder exists, and create if it doesn't.
    fileFuncs.checkFolder(api_constants.itemThumbnailPath);

    // For each item in the item JSON, save the thumbnail.
    for (itemKey in dataJSON) {
        if (dataJSON.hasOwnProperty(itemKey)) {
            item = dataJSON[itemKey];

            // The image are saved as #{id}.png
            imageName = item['id'] + '.png';

            imageURL = api_constants.itemThumbnailURL + imageName;

            // Retrieve the file from the API.
            fileFuncs.retrieveAndProcessImage(imageURL, function (fileName, image) {
                filePath = api_constants.itemThumbnailPath + fileName;

                fs.writeFile(filePath, image, { encoding: 'binary' }, function (err) {
                    if (err) {
                        console.log("\t" + fileName + " already exists.");
                        return;
                    }

                    console.log("\tThumbnail " + fileName + " was saved.");
                })
            });
        }
    }
}

我不希望每次都要部署到Heroku,每次我都想看到代码中的变化是什么样子。是否有任何解决方案可以让我在每个请求之间留出一些时间?或者,只需做一些可以让我在不挂断internet的情况下执行大量请求的事情。

执行大量请求的代码在哪里?您可以使用setTimeout函数添加延迟。更新了代码段。我研究了setTimeout,但如果可能的话,我不希望创建一个超时,因为它在我部署代码时有效,只有在本地测试时才失败。