How to use js to read the contents of a txt file and assign the contents to variables (of type string)

how to use js to read the contents of a txt file and assign the contents to a variable (of type string)

Dec.12,2021

it is easiest to directly call the method of node.js 's built-in module fs to read the contents of the file:

const fs = require("fs");
const path = "./xxx.txt"; // text
const isExist = fs.existsSync(path); // 
const data = isExist ? fs.readFileSync(path, "utf-8") : "";
console.log(data);
Menu