简体   繁体   English

使用Node.js成功http请求后记录图像

[英]Log images after successful http request using Node.js

I'm creating a script that will make a request 2 times per second to a localserver of cameras network and after it gets a positive response that camera detected someone I want to log three images. 我正在创建一个脚本,该脚本将每秒向摄影机网络的本地服务器发出2次请求,并且在收到肯定响应后,摄影机检测到我要记录三张图像的请求。

In the json config file I have the triggerURL of the server, the interval port, the dataDir where logged images should be saved and a track array which contains the url of those images and the fileName they should receive. 在json配置文件中,我具有服务器的triggerURL间隔端口,应在其中保存记录的图像的dataDir和一个包含这些图像的url和应接收的文件名track数组。

This is the code of the script I use after reading the JSON file: 这是我在读取JSON文件后使用的脚本代码:

var configGet = {
    host: config.triggerURL
    , port: config.interval
    , method: 'GET'
};

setInterval(function () {
    var request = http.request(configGet, function (response) {
        var content = "";

        // Handle data chunks
        response.on('data', function (chunk) {
            content += chunk;
        });

        // Once we're done streaming the response, parse it as json.
        response.on('end', function () {
            var data = JSON.parse(response);

            if (data.track.length > 0) {
                //log images
                var download = function (uri, filename, callback) {
                    request.head(uri, function (err, res, body) {
                        request(uri)
                            .pipe(fs.createWriteStream(filename))
                            .on('close', callback);
                    });
                };
                for (var image in data.track) {
                    var path = config.dataDir + '/' + image.fileName
                    download(image.url, path.format(config.timestamp), function () {
                        console.log('done');
                    });
                }
            }
        });

        // Report errors
        request.on('error', function (error) {
            console.log("Error while calling endpoint.", error);
        });

        request.end();
    }, 500);
});

I have the following questions: 我有以下问题:

  1. This method produces some kind of error with the download process of the images.Can you identify it? 这种方法会在图像的下载过程中产生某种错误,可以识别吗?

  2. Is there a better way of doing this process? 有没有更好的方法来执行此过程?

  1. Without running the code or deeper inspection; 无需运行代码或进行更深入的检查; should not "data = JSON.parse(response)" rather be "data = JSON.parse(content)"? 应该不是“数据= JSON.parse(响应)”而是“数据= JSON.parse(内容)”? Also, if data is undefined or does not contain "track" the "if (data.track.length > 0)" will throw an error. 另外,如果数据未定义或不包含“磁道”,则“ if(data.track.length> 0)”将引发错误。 This can be fixed with "if (data && data.track && data.track.length > 0)". 可以使用“ if(data && data.track && data.track.length> 0)”修复此问题。
  2. I can not think of a very much better way. 我想不出更好的方法。 I would break it up more in functions to make the code more clear though. 我将在函数中对其进行更多分解,以使代码更加清晰。

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

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