Answers for "node js open a new file"

1

read file in nodejs using fs

let myFile = "./myText.txt";
const fs = require("fs");
		
app.all('/test', async (req, res) => {
	try {
		const readData = fs.readFileSync(myFile, 'utf8');
		if (readData) {
			res.send(readData)
		}
	} catch (error) {
		res.send("something is wrong", error)
	}
})
Posted by: Guest on January-07-2021
3

fs create or edit file

//Edit file or creates if doesnt exist
 const fs = require('fs');
 fs.appendFile(‘<fileName>’,<content>,callbackFunction)
Posted by: Guest on April-01-2020
0

nodejs open file

fs = require('fs')
fs.readFile('/etc/hosts', 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  console.log(data);
});
Posted by: Guest on December-05-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language