Last active
November 6, 2018 15:10
-
-
Save kvnam/ddfc8f4a601c99e1b259faa561162476 to your computer and use it in GitHub Desktop.
fs Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
module.exports.loadHome = (req, res) => { | |
res.render('index', {title: 'fs Module Node.js', message: 'File System Module Tutorial'}); | |
}; | |
module.exports.getfd = (req, res) => { | |
fs.open(__dirname + '/files/TestFile1.txt', 'r', (err, fd) => { | |
if(err){ | |
return res.render('index', {title: 'Error', message: 'There was an error opening the file', content: err}); | |
} | |
//You can now use fd to read, write or get stats about the file | |
fs.readFile(fd, (err, data) => { | |
if(err){ | |
return res.render('index', {title: 'Error', message: 'There was an error reading the file', content: err}); | |
} | |
//Always close the file descriptor as this may lead to memory leaks | |
fs.close(fd, (err) => { | |
if(err){ | |
return res.render('index', {title: 'Error', message: 'There was an error closing the file', content: err}); | |
} | |
}); | |
res.render('index', {title: 'fs Module Node.js', message: 'File Descriptor for the file is ' + fd, content: data}); | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
let pathToFile = path.join(__dirname, '../files/', 'TestFile1.txt'); | |
let readStream = fs.createReadStream(pathToFile); | |
readStream.on('open', (fd) => { | |
console.log('Stream opened'); | |
}); | |
readStream.on('data', (chunk) => { | |
console.log(`Data chunk is ${chunk}`); | |
}); | |
readStream.on('close', () => { | |
console.log('Stream closed'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
fs.open(path.join(__dirname, '../files/', 'TestFile1.txt'), 'r', (err, fd) => { | |
if(err){ | |
console.log('Error opening file'); | |
console.log(err); | |
} | |
let stats = fs.fstat(fd, (err, stats) => { | |
if(err){ | |
console.log('Error finding stats'); | |
} | |
console.log(stats); | |
fs.close(fd, (err) => { | |
if(err){ | |
console.log('Error closing file'); | |
} | |
}) | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
let pathToFile = path.join(__dirname, '../files/', 'TestFile1.txt'); | |
let watcher = fs.watch(pathToFile, (eventType, filename) => { | |
console.log(`Event type is ${eventType}`); //This can either be change or rename | |
if(filename){ | |
console.log(`Filename is ${filename}`); //This may not always be set depending on the OS | |
} | |
}); | |
setTimeout(() => { | |
console.log('Closing watcher'); | |
watcher.close(); | |
}, 5000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
let pathToFile = path.join(__dirname, '../files/', 'TestFile2.txt'); | |
let writeStream = fs.createWriteStream(pathToFile); | |
writeStream.on('open', (fd) => { | |
console.log('Stream opened'); | |
writeStream.write('THis is some extra text', (err) => { | |
if(err){ | |
console.log('Error writing data'); | |
} | |
console.log('Data written'); | |
}); | |
}); | |
writeStream.on('finish', () => { | |
console.log('Write complete'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Stats { | |
dev: 2114, | |
ino: 48064969, | |
mode: 33188, | |
nlink: 1, | |
uid: 85, | |
gid: 100, | |
rdev: 0, | |
size: 527, | |
blksize: 4096, | |
blocks: 8, | |
atimeMs: 1318289051000.1, | |
mtimeMs: 1318289051000.1, | |
ctimeMs: 1318289051000.1, | |
birthtimeMs: 1318289051000.1, | |
atime: Mon, 10 Oct 2011 23:24:11 GMT, | |
mtime: Mon, 10 Oct 2011 23:24:11 GMT, | |
ctime: Mon, 10 Oct 2011 23:24:11 GMT, | |
birthtime: Mon, 10 Oct 2011 23:24:11 GMT } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment