#How to read write files synchronously with fs
1 2 3 4 5 6 7 8 9 10 11 |
const fs = require('fs') const str = '1Hello world!' // save data fs.writeFileSync(`${__dirname}/data.html`, str) // read data const d = fs.readFileSync(`${__dirname}/data.html`, 'utf8') console.log('d = ', d) // d = 1Hello world! |
#How to read write files asynchronously with fs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
const fs = require('fs') const str = 'Hello world!' // save data fs.writeFile(`${__dirname}/data.html`, str, err => { if (err) throw err console.log('saved'); }) // read data fs.readFile(`${__dirname}/data.html`, 'utf8', (err, data) => { if (err) throw err console.log('data = ', data); // data = Hello world! }) |
#How to check existence of files Sometimes we have to know whether a file exists or not. To do this we’ll be using a node.js fs module. We could check synchronously by using and…