1const fs = require('fs')
2
3try {
4 const data = fs.readFileSync('/Users/joe/test.txt', 'utf8')
5 console.log(data)
6} catch (err) {
7 console.error(err)
8}
1const fs = require('fs');
2
3fs.writeFile("/tmp/test", "Hey there!", function(err) {
4 if(err) {
5 return console.log(err);
6 }
7 console.log("The file was saved!");
8});
9
10// Or
11fs.writeFileSync('/tmp/test-sync', 'Hey there!');
1const fs = require('fs');
2
3fs.readFile('/Users/joe/test.txt', 'utf8' , (err, data) => {
4 if (err) {
5 console.error(err);
6 return
7 }
8 console.log(data);
9});
1let myFile = "./myText.txt";
2const fs = require("fs");
3
4app.all('/test', async (req, res) => {
5 try {
6 const readData = fs.readFileSync(myFile, 'utf8');
7 if (readData) {
8 res.send(readData)
9 }
10 } catch (error) {
11 res.send("something is wrong", error)
12 }
13})