簡體   English   中英

如何從文件系統讀取數據並將完整的消息發送給客戶端?

[英]How to read data from file System and send complete message to client?

在我的實現中,如果用戶從客戶端搜索,我想檢查字符串是否是文件的一部分。 我正在將該行數據發送回客戶端作為響應。

我的問題是假設用戶要搜索testid012 因此,使用我當前的代碼,它只會找到包含搜索字符串的單行。 相反,我想發送包含搜索字符串的多行響應,在本例中為testid012 這可行嗎?

searchService.js

fs.readFile('logs/dit/' + logfile.filename, 'utf8', function (err, data) {
            if (err) {
                return done(err);
            }
            var lines = data.split('\n'); // get the lines
            lines.forEach(function(line) { // for each line in lines
                if (line.indexOf(searchStr) != -1) { // if the line contain the searchSt
                    results.push({
                    filename:logfile.filename,
                    value:line
                    });
                }
            });
            // when you are done reading the file
            done();
        });

DIT / server.log中

testid012 Lorem test Ipsum is simply dummy text text of the printing  and typesetting industrytestid Lorem test Ipsum is simply dummy text text of the printing and typesetting industrytestid Lorem test Ipsum is simply dummy text text of the printing  and typesetting industry
Lorem test Ipsum is simply dummy text text of the printing  and typesetting industry,testid Lorem test Ipsum is simply dummy text text of the printing  and typesetting industry

testid013 Lorem test Ipsum is simply dummy text text of the printing and typesetting industry,testid Lorem test Ipsum is simply dummy text text of the printing  and typesetting industrytestid Lorem test Ipsum is simply dummy text text of the printing  and typesetting industry
Lorem test Ipsum is simply dummy text text of the printing  and typesetting industry

您的解決方案可以工作,但是只得到一行的原因是您要按行尾拆分輸入。 似乎您需要弄清楚事件是如何分隔的,並更改data.split()調用。

如果有數據啟動每個事件,例如[EVENT] This is an event則使用正則表達式對此進行分割。 data.split('[EVENT] ')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM