I am reading a book, where the author asked to improve the code by handling the below 2 questions:In the file-watching examples, what happens if the target file doesn’t exist?What happens if a file being watched gets deleted?I handled the first question. But not able to solve the Q #2. Can anyone teach me a good technique on this #2?My code:const fs = require('fs'); const filename = process.argv[2]; if(!filename) { throw Error('A file to watch must be specified!'); } if (!fs.existsSync(filename)) { throw Error(`The target file ${filename} doesn't exist.`) } fs.watch( filename, (eventType, file) => console.log(`File ${file} changed!`, `Event received: ${eventType}`) ); console.log(`Now watching ${filename} for changes...`);
Submitted May 11, 2018 at 11:37AM by arup_r
No comments:
Post a Comment