Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

read file node

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

node open file

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());
});
Comment

read a file nodejs

const fs = require('fs');

fs.readFile('my-file.txt', 'utf8', function(err, data) {
    if (err) throw err;
    console.log(data);
});
Comment

Reading files with Node.js

JS
copy
const fs = require('fs')

fs.readFile('/Users/joe/test.txt', 'utf8' , (err, data) => {
  if (err) {
    console.error(err)
    return
  }
  console.log(data)
})
Comment

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)
	}
})
Comment

nodejs open file

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

read files in node js

const fs = require("fs");
const text = fs.readFileSync("dele.txt", "utf-8")
console.log("The content of the file is")
console.log(text);
Comment

open file method in node js

var fs = require("fs");
  
// Asynchronous - Open File
console.log("opening file!");
fs.open('input.txt', 'r+', function(err, fd) {
   if (err) {
      return console.error(err);
   }
   console.log("File open successfully");     
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to calculate time taken for ajax call in javascript 
Javascript :: jquery xpath 
Javascript :: round innerhtml value down javascript 
Javascript :: round down javascript 
Javascript :: mongoose limit skip 
Javascript :: jquery select input value empty and hasclass 
Javascript :: node express dynamic route and error handler 
Javascript :: javascript async await not waiting 
Javascript :: key js 
Javascript :: how to find the radius of a loacation in node js 
Javascript :: how to make if method inside an if methen in fandom 
Javascript :: ajaxsetup beforesend 
Javascript :: how to add all files in a director to an array in javascript 
Javascript :: recaptcha v3 
Javascript :: react state management 
Javascript :: switch case statement in javascript 
Javascript :: https://jsonplaceholder.typicode.com/albums/1 
Javascript :: sails setup 
Javascript :: load jquery in console 
Javascript :: localstorage nextjs 
Javascript :: nodejs check if file is running on server or client 
Javascript :: html js hide or show iframe 
Javascript :: itsycal homebrew 
Javascript :: redirect with data jquery 
Javascript :: simultaneos mouse buttons clicked js 
Javascript :: execcommand image 
Javascript :: fcm node 
Javascript :: javascript add to undefined 
Javascript :: command reboot android app react native adb command 
Javascript :: how to use react memo hooks 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =