javascript - Get line from file - NodeJS function -


how write function returns line file nodejs? program runs in loop, , each time call function should return new string. , if file ends begins again, first line.

function takes randomly, how consistently?

 var fs = require('fs');   // random line file  function getrandsocks() {    var socks = fs.readfilesync('socks.txt').tostring().split("\n");    var randsock = socks[math.floor(math.random()*socks.length)];    return randsock.split(':');  }   function loop() {    // ...    var socks = getrandsocks();    console.log('host: '+socks[0]+'port :'+socks[1]);    //...    loop();  } 

perhaps help. gets next line , wraps start. based on given getrandsocks function minimal change.

var current_line = 0; function getnextlines() {    var socks = fs.readfilesync('socks.txt').tostring().split("\n");    var randsock = socks[current_line % socks.length];    current_line = (current_line + 1) % socks.length    return randsock.split(':'); } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -