// load fs const fs = require("fs"); // read the file const content = fs.readFileSync("./my_file.txt"); // print it console.log(content.toString());
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);
const fs = require('fs'); fs.readFile('/Users/joe/test.txt', 'utf8' , (err, data) => { if (err) { console.error(err); return } console.log(data); });
var content; fs.readFile('./Index.html', function read(err, data) { if (err) { throw err; } content = data; }); console.log(content);
// macOS, Linux, and Windows fs.readFileSync('<directory>'); // => [Error: EISDIR: illegal operation on a directory, read <directory>] // FreeBSD fs.readFileSync('<directory>'); // => <data>