node.js & fs & file read & file write
const fs = require("fs");// absolute pathfs.open("/open/some/file.txt", "r", (err, fd) => { if (err) { throw err; } fs.close(fd, (err) => { if (err) { throw err; } });});// relative pathfs.open("file.txt", "r", (err, fd) => { if (err) { throw err; } fs.close(fd, (err) => { if (err) { throw err; } });});
readline
const readline = require('readline');const rl = readline.createInterface({ input: process.stdin, output: process.stdout});rl.question('What do you think of Node.js? ', (answer) => { // TODO: Log the answer in a database console.log(`Thank you for your valuable feedback: ${answer}`); rl.close();});
const fs = require("fs");const readline = require('readline');const rl = readline.createInterface({ input: process.stdin, output: process.stdout});// absolute path// fs.open("/open/some/file.txt", "r", (err, fd) => {// if (err) {// throw err;// }// fs.close(fd, (err) => {// if (err) {// throw err;// }// });// });// relative pathfs.open("file.txt", "r", (err, fd) => { if (err) { throw err; } console.log(`fd =`, fd);// 3 rl.on('line', (fd) => { console.log(`Received: ${fd}`); }); fs.close(fd, (err) => { if (err) { throw err; } });});
bug
write & read
// preview.jsconst fs = require("fs");const readline = require("readline");fs.writeFile("file.js", `export const APP_ENV = "preview";`, function(err) { if(err) { console.log(err); return err; } let env = "preview"; console.log("The file was saved!", env);});let rd = readline.createInterface({ input: fs.createReadStream("file.js"), output: process.stdout, console: false});rd.on("line", function(line) { console.log(`line =`, line);});// fs.writeFile("file.txt", "prview", function(err) {// if(err) {// console.log(err);// return err;// }// console.log("The file was saved!");// });// global.APP_ENV = "testing";// let APP_ENV = global.APP_ENV;// console.log(`APP_ENV =`, APP_ENV);// let rd = readline.createInterface({// input: fs.createReadStream('file.txt'),// output: process.stdout,// console: false// });// rd.on('line', function(line) {// console.log(`line =`, line);// });
// testing.jsconst fs = require("fs");const readline = require("readline");fs.writeFile("file.js", `export const APP_ENV = "testing";`, function(err) { if(err) { console.log(err); return err; } let env = "testing"; console.log("The file was saved!", env);});// const rl = readline.createInterface({// input: process.stdin,// output: process.stdout// });// absolute path// fs.open("/open/some/file.txt", "r", (err, fd) => {// if (err) {// throw err;// }// fs.close(fd, (err) => {// if (err) {// throw err;// }// });// });// global.APP_ENV = "testing";// let APP_ENV = global.APP_ENV;// console.log(`APP_ENV =`, APP_ENV);// relative path// fs.open("file.js", "r", (err, fd) => {// if (err) {// throw err;// }// console.log(`fd =`, fd);// 3// fs.close(fd, (err) => {// if (err) {// throw err;// }// });// });let rd = readline.createInterface({ input: fs.createReadStream("file.js"), output: process.stdout, console: false});rd.on("line", function(line) { console.log(`line =`, line);});
// file.jsexport const APP_ENV = "testing";