Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.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
Javascript 函数检查外部托管的文本文件是否包含动作缓慢的字符串-节点_Javascript_Node.js - Fatal编程技术网

Javascript 函数检查外部托管的文本文件是否包含动作缓慢的字符串-节点

Javascript 函数检查外部托管的文本文件是否包含动作缓慢的字符串-节点,javascript,node.js,Javascript,Node.js,我有以下代码,它将从外部服务器检索文本文件,并在文件中搜索特定字符串 功能: function checkStringExistsInFile(String, cb) { var opt = { host: 'site.com', port: 80, path: '/directory/data.txt', method: 'GET', }; var request = http.request(opt, function(response){ resp

我有以下代码,它将从外部服务器检索文本文件,并在文件中搜索特定字符串

功能:

function checkStringExistsInFile(String, cb) {
var opt = {
    host: 'site.com',
    port: 80,
    path: '/directory/data.txt',
    method: 'GET',
};

var request = http.request(opt, function(response){
    response
    .on('data', function(data){
        var string = data+"";
        var result = ((string.indexOf(" "+ String +" ")!=-1)?true:false);
        cb(null, result);
    })
    .on('error', function(e){
        console.log("-ERR: Can't get file. "+JSON.stringify(e));
        if(cb) cb(e);
    })
    .on('end', function(){
        console.log("+INF: End of request");
    });
});
request.end();
}
我在这里调用函数,并对结果进行处理。

checkStringExistsInFile(String, function(err, result){
    if(!err) {
        if(result) {
            //there was a result
        } else {
            //string not present in file
        }
    } else {
        // error occured
    }
});
这在开始时效果很好(小文本文件),但是我的文本文件越来越大(4000个字符以上),这已经不起作用了

我能做些什么来解决这个问题?我是否应该先将临时文件保存在服务器上,然后将文件作为流打开


如果您能举一个相关的例子来支持您的答案,我们将不胜感激。提前谢谢

文档:

function checkStringExistsInFile(String, cb) {
var opt = {
    host: 'site.com',
    port: 80,
    path: '/directory/data.txt',
    method: 'GET',
};

var request = http.request(opt, function(response){
    response
    .on('data', function(data){
        var string = data+"";
        var result = ((string.indexOf(" "+ String +" ")!=-1)?true:false);
        cb(null, result);
    })
    .on('error', function(e){
        console.log("-ERR: Can't get file. "+JSON.stringify(e));
        if(cb) cb(e);
    })
    .on('end', function(){
        console.log("+INF: End of request");
    });
});
request.end();
}
如果您附加了一个数据事件监听器,那么它会将流切换到流模式,数据一可用就会被传递给处理程序

如果您只想尽快从流中获取所有数据,这是最好的方法

数据事件会在有数据时立即发出,即使流没有完全加载。因此,代码只需在流的第一行中查找字符串,然后回调

怎么办?

您的函数应该只在
end()
事件上调用callback,或者在找到某个内容后立即调用callback

function checkStringExistsInFile(String, cb) {
  var opt = {
    host: 'site.com',
    port: 80,
    path: '/directory/data.txt',
    method: 'GET',
  };

  var request = http.request(opt, function(response){
    var result = false;
    response
    .on('data', function(data){
        if(!result) {
            var string = data+"";
            result = (string.indexOf(" "+ String +" ")!=-1);
            if(result) cb(null, result);
        }
    })
    .on('error', function(e){
        console.log("-ERR: Can't get file. "+JSON.stringify(e));
        if(cb) cb(e);
    })
    .on('end', function(){
        console.log("+INF: End of request");
        cb(null, result)
    });
  });
  request.end();
}

“不再工作”是什么意思?发生了什么而不是你期望的?它会引起错误吗?回电永远不会响吗?只是比以前慢了一些?具体问题是什么?
(条件?真:假)
有些多余,因为(条件)已经返回
true
(如果为真)和
false
(如果为假)。@Alex响应时间太长了,我在执行脚本时出现502错误。@Mdlc我刚刚做了一次编辑,以确保它只回调一次。太好了,代码正在按预期运行,非常感谢您的帮助!