Losowa linijka z pliku Node.js

krytyYT | 2021-02-17 10:25:19 UTC | #1

Cześć, mam pewien kod

app.get('/',(req,res)=>{
    fs.readFile("plik.txt", function(err, data){
        if(err) throw err;
        let lines = data+=''.toString().split('\n');
        let line = lines[Math.floor(Math.random()*lines.length)];
        console.log(line)
        res.render("index", {image: line})
    })
})

ma on zdobyć losową linijkę z pliku ~~przynajmniej tak na stacku piszą~~ a losuję on tylko jeden znak.

Wie ktoś jak to naprawić?


Wright | 2020-09-08 12:32:35 UTC | #2

app.get('/', (req, res) => {
    fs.readFile('plik.txt', function (err, data) {
        if (err) throw err;
        let lines = data.toString().split('\n');
        let line = lines[Math.floor(Math.random() * lines.length)];
        console.log(line);
        res.render('index', {image: line})
    });
});

system | 2020-10-10 12:26:09 UTC | #3

Ten temat został automatycznie zamknięty 32 dni po ostatnim wpisie. Tworzenie nowych odpowiedzi nie jest już możliwe.