JS

HOW TO READ FILES IN NODEJS

require("fs") .readFile("FILE.TXT", "utf8", (err, data) => {   console.log(data); });

READ FILE (ASYNC)

01

var data = require("fs") .readFileSync("FILE.TXT", "utf8");

READ FILE (SYNC)

02

const fs = require("fs"),           rl = require("readline"); const reader = rl.createInterface({   input: fs.createReadStream("FILE.TXT") }); reader.on("line", (r) => { console.log(r); });

READ FILE LINE-BY-LINE

03

require("fs") .readFile("FILE.TXT", "utf8", (err, data) => {   data = data.split("\r\n"); });

READ INTO ARRAY

04

require("https") .get("https://site.com", (res) => {   let data = "";   res.on("data", (dat) => { data += dat; });   res.on("end", () => { console.log(data); });  });

READ REMOTE SERVER

05