2016-03-06 77 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'并按照相应的更新代码,让我知道你是否卡在任何地方 –