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

how to read files in node

var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
  fs.readFile('index.html', function(err, data) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(data);
    return res.end();
  });
}).listen(8080);
Comment

PREVIOUS NEXT
Code Example
Javascript :: fs.appendFileSync in nodejs 
Javascript :: refresh a single component 
Javascript :: how to add element in json object 
Javascript :: collapse in angular 4 
Javascript :: js replace greek accents 
Javascript :: how to insert div around element in javascript 
Javascript :: get index of item with attribute javascript 
Javascript :: how to make button in react js 
Javascript :: group by in javascript 
Javascript :: js comments 
Javascript :: how to send the mail using node with template 
Javascript :: moment.set 
Javascript :: get gravatar image 
Javascript :: days between two dates 
Javascript :: es6 spread 
Javascript :: jquery create array 
Javascript :: angular selector 
Javascript :: check the number is palindrome or not 
Javascript :: jquery not equal 
Javascript :: react icon import 
Javascript :: Sort an array by both ascending and descending order in js 
Javascript :: aws secret manager nodejs 
Javascript :: no routes matched location / react router 
Javascript :: pylint vscode disable max line length 
Javascript :: js object getter 
Javascript :: merge 2 arrays jquery 
Javascript :: convert object to url javascript 
Javascript :: full month name using moment 
Javascript :: xmlhttprequest object 
Javascript :: angular subscribe on value change 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =