I am trying to randomly print a line from a text file. I am reading the text file and return a random line from it. I passed it to the res.send() but it doesn't show up on the screen. It shows up in the log file, though. Any ideas on what is going wrong here?
This is the javascript code that I am running:
var express=require('express');
var app=express();
var port = process.env.PORT || 3000
app.get('/', function (req, res) {
res.send(getRandomLine('listofquestions.txt'));
})
/*--------------------Routing Over----------------------------*/
function getRandomLine(filename){
var fs = require('fs');
fs.readFile(filename, function(err, data) {
if(err) throw err;
var array = data.toString().split("\n");
var length = Math.floor(Math.random() * array.length) + 1 ;
console.log(array[length]);
return (array[length]);
});
}
app.listen(port, function () {
console.log(`Server listening on port ${port}!`)
});
This is the file I am using to read from:
What is possible?
What if it works out exactly as you want it to?
What is the dream?
What is exciting to you about this?
What is the urge?
What does your intuition tell you?