2016-03-06 90 views
0

我目前有一個問題,我試圖逐行讀取文件,然後將所述文件的內容輸出到由nodejs託管的網頁上。我試圖使用一個while循環,但林不知道它的工作,我真的不知道什麼其他方式去做。我可以使用一些幫助。 NodeJS - 通過線路功能讀取線路+雖然循環

var sys = require('util'); 
 
var exec = require('child_process').exec; 
 
//Find Audio Files in Audio Directory 
 
exec("~/music_Controller/./music_Find.sh"); 
 

 
var contents = ''; 
 

 
var fs = require('fs'); 
 

 
//Reads file defined on input (readLines(input, func);) and reads them line by line 
 
function readLines(input, func) { 
 
    var remaining = ''; 
 

 
    input.on('data', function(data) { 
 
    remaining += data; 
 
    var index = remaining.indexOf('\n'); 
 
    while (index > -1) { 
 
     var line = remaining.substring(0, index); 
 
     remaining = remaining.substring(index + 1); 
 
     func(line); 
 
     index = remaining.indexOf('\n'); 
 
    } 
 
    }); 
 

 
    input.on('end', function() { 
 
    if (remaining.length > 0) { 
 
     func(remaining); 
 
    } 
 
    }); 
 
} 
 

 
function func(data) { 
 
    contents = data; 
 
} 
 

 

 

 

 
readLines(input, func); 
 

 

 

 
function puts(error, stdout, stderr) { sys.puts(stdout) } 
 

 
var http = require('http'); 
 
var server = http.createServer(function(request, response) { 
 
\t var input = fs.createReadStream('/root/music_Controller/songs.txt'); 
 
    while(readLines(input, func)){ 
 
    \t response.write(contents); 
 

 
    } 
 

 
    response.writeHead(200, {"Content-Type": "text/html"}); 
 
    response.write("<!DOCTYPE html>"); 
 
    response.write("<html>"); 
 
    response.write("<head>"); 
 
    response.write("<title>Hello World Page</title>"); 
 
    response.write("</head>"); 
 
    response.write("<body>"); 
 
    response.write("Hello World!"); 
 
    response.write("</body>"); 
 
    response.write("</html>"); 
 
    response.end(); 
 

 
    //Execute Shell Script/Command 
 
    exec("play ~/music_Controller/song.mp3", puts); 
 
    console.log("exec"); 
 
}); 
 

 
server.listen(8911); 
 
console.log("Server is listening");

感謝, 賢者

回答

3

下面的選項讓你無論是看整個文件累積結果和發送作爲一個響應網頁或者,如果你有網絡套接字打開或其他形式的與客戶端連接,您可以發送如果需要的情況下讀取和處理的earch行。

選項1:

Tail = require('tail').Tail; 

tail = new Tail("/root/music_Controller/songs.txt", "\n", {}, true); 

tail.on("line", function(data) { 
    console.log(data); 
}); 

tail.on("error", function(error) { 
    console.log('ERROR: ', error); 
}); 

選項2:npm install linebyline

var readline = require('linebyline'), 
rl = readline('./somefile.txt'); 
rl.on('line', function(line, lineCount, byteCount) { 
    // do something with the line of text 
}) 
.on('error', function(e) { 
    // something went wrong 
}); 

有關完整有效的解決方案得到這個Github Repo和運行line_by_line.js

快樂幫助!

+0

當我在代碼中執行選項2時發生錯誤。 readline.js:67 this.enabled = output.isTTY; ^ TypeError:無法在服務器上的Object.createInterface(readline.js:38:10) 上的新接口(readline.js:67:24) 處讀取屬性'isTTY'null 。在HTTPParser.onIncoming(http.js:1610:12)上的Server.emit(events.js:70:17) (在HTTPParser.parserOnHeadersComplete中爲(/root/music_Controller/music.js:50:21) on.HeadersComplete](http.js:91:29) at Socket.ondata(http.js:1506:22) at TCP.onread(net.js:374:27) –

+1

請'npm install linebyline'並按照相應的更新代碼,讓我知道你是否卡在任何地方 –