Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
46
rated 0 times [  48] [ 2]  / answers: 1 / hits: 26116  / 7 Years ago, tue, april 4, 2017, 12:00:00

I using below code for reading file from my local system:



var fs = require('fs');            
var text = fs.readFileSync(./men.text);
var textByLine = text.split(n)
console.log(textByLine);

More From » node.js

 Answers
20

NOTE: fs is a nodejs module, you cannot use it in Browser.



Import the fs module,



readFileSync will provide you the Buffer



to use the split() function you have to convert the Buffer into String



var fs = require('fs')

var text = fs.readFileSync(./men.text);
var string = text.toString('utf-8') // converting the Buffer into String

var textByLine = string.split(n)
console.log(textByLine);





UPDATE






Server-Side



fs is a nodejs built-in module, you cannot use it in Browser(Client-Side). Use fs in server-side to do the manipulation, get the data and format in required type, then you can render it with html, ejs many more.. templating engines



Here i have created a Nodejs Server using express, and from the browser hit the http://localhost:8000/ you will get the Array of Data



You can format your data and render it with the .ejs or html files using res.render



app.js



var express = require('express');
var app = express();
var fs = require('fs')

app.get('/', function (request, response) {
var text = fs.readFileSync(./men.text);
var string = text.toString('utf-8')

var textByLine = string.split(n)
console.log(textByLine);
response.send(textByLine);
});

app.listen('8000');





Dummy Output:



enter


[#58278] Sunday, April 2, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
beatrizrheaq

Total Points: 73
Total Questions: 89
Total Answers: 107

Location: Jersey
Member since Fri, Oct 1, 2021
3 Years ago
;