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

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 remove item from asyncstorage 
Javascript :: get attribute value jquery href 
Javascript :: jquery disable checkbox 
Javascript :: sequelize order by date 
Javascript :: float right react native 
Javascript :: js go to previous page 
Javascript :: javascript check if undefined 
Javascript :: js get query param 
Javascript :: react native duplicate resources: Android 
Javascript :: one year ago javascript date 
Javascript :: document on click 
Javascript :: how to get the height of window in javascript 
Javascript :: javascript change meta tag 
Javascript :: store data in localstorage javascript 
Javascript :: how to convert string to int js 
Javascript :: jquery extract number from string 
Javascript :: execute javascript on page load jquery 
Javascript :: install react 
Javascript :: angular 11 on hover 
Javascript :: Root composer.json requires tymon/jwt-auth ^0.5.12 - satisfiable by tymon/jwt-auth[0.5.12]. 
Javascript :: remove previous datatable instance 
Javascript :: javascript get attribute 
Javascript :: how to use jquery in chrome dev tools 
Javascript :: js number remove last 2 characters 
Javascript :: javascript code for year in html 
Javascript :: how to pad string js 
Javascript :: update to specific version of node brew 
Javascript :: regex to get items between quotes 
Javascript :: babel cdn react 
Javascript :: jquery add attribute 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =