node read file
snippet in javascript

read file node

user915

// load fs
const fs = require("fs");
// read the file
const content = fs.readFileSync("./my_file.txt");
// print it
console.log(content.toString());

node open file

user5223

const fs = require("fs");

// __dirname means relative to script. Use "./data.txt" if you want it relative to execution path.
fs.readFile(__dirname + "/data.txt", (error, data) => {
    if(error) {
        throw error;
    }
    console.log(data.toString());
});

node read file

user746

var http = require('http');
var fs = require('fs');
http.createServer(function 
  (req, res) {
  fs.readFile('demofile1.html', function(err, data) {

      
  res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    
  res.end();
  });
}).listen(8080); 

readfile nodejs

user7340

var content;
fs.readFile('./Index.html', function read(err, data) {
    if (err) {
        throw err;
    }
    content = data;
});
console.log(content);

nodejs open file

user5700

fs = require('fs')
fs.readFile('/etc/hosts', 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  console.log(data);
});