Created
July 2, 2013 05:26
-
-
Save amirkheirabadi73/5906951 to your computer and use it in GitHub Desktop.
FS System ...
Read And Write In txt ANd Json File
This file contains hidden or 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
var path = require('path'); | |
path.normalize('/a/.///b/d/../c/') | |
'/a/b/c/' | |
// '.' for 'this directory' and '..' for 'one level up' | |
var path = require('path'); | |
var url = '/index.html'; | |
path.join(process.cwd(), 'static', url); | |
'/home/nico/static/index.html' | |
var path = require('path') | |
var a = '/a/b/c.html' | |
path.basename(a) | |
'c.html' | |
path.extname(a) | |
'.html' | |
path.dirname(a) | |
'/a/b' | |
var path = require('path') | |
var a = '/a/b/c.html' | |
path.basename(a, path.extname(a)) | |
'c' | |
var path = require('path') | |
path.exists('/etc', function(exists){console.log("Does the file exist?", exists)}) | |
Does the file exist? true | |
path.existsSync('/etc') | |
true |
This file contains hidden or 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
var fs=require("fs"); | |
console.log("Starting ..."); | |
var content = fs.readFileSync("./config.json"); | |
console.log("content :" + content ); | |
var config = JSON.parse(content); | |
console.log("config :",config); | |
console.log("username :" , config.usernam); | |
console.log("name : " , config.name); |
This file contains hidden or 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
fs = require('fs'); | |
fs.readdir(process.cwd(), function (err, files) { | |
if (err) { | |
console.log(err); | |
return; | |
} | |
console.log(files); | |
}); |
This file contains hidden or 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
npm install findit | |
//This sets up the file finder | |
var finder = require('findit').find(__dirname); | |
//This listens for directories found | |
finder.on('directory', function (dir) { | |
console.log('Directory: ' + dir + '/'); | |
}); | |
//This listens for files found | |
finder.on('file', function (file) { | |
console.log('File: ' + file); | |
}); |
This file contains hidden or 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
var fs=require("fs"); | |
console.log("Starting"); | |
fs.readFile("./text.txt" , function(error,data){ | |
console.log("Content" + data); | |
}); | |
console.log("finish ...."); |
This file contains hidden or 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
var fs=require("fs"); | |
console.log("Starting"); | |
var con=fs.readFileSync("./text.txt"); | |
console.log("Content :" + con); | |
console.log("finish ...."); |
This file contains hidden or 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
var fs=require("fs"); | |
console.log("Starting"); | |
fs.writeFileSync("writesync.txt", "Hello World !!! My name Is Amir"); | |
console.log("Finished"); |
This file contains hidden or 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
var fs = require('fs'); | |
var myOptions = { | |
name: 'Avian', | |
dessert: 'cake' | |
flavor: 'chocolate', | |
beverage: 'coffee' | |
}; | |
var data = JSON.stringify(myOptions); | |
fs.writeFile('./config.json', data, function (err) { | |
if (err) { | |
console.log('There has been an error saving your configuration data.'); | |
console.log(err.message); | |
return; | |
} | |
console.log('Configuration saved successfully.') | |
}) | |
And Now ReadFrom File : | |
var fs = require('fs'); | |
var data = fs.readFileSync('./config.json'), | |
myObj; | |
try { | |
myObj = JSON.parse(data); | |
console.dir(myObj); | |
} | |
catch (err) | |
console.log('There has been an error parsing your JSON.') | |
console.log(err); | |
} |
This file contains hidden or 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
npm install nconf | |
var nconf = require('nconf'); | |
nconf.use('file', { file: './config.json' }); | |
nconf.load(); | |
nconf.set('name', 'Avian'); | |
nconf.set('dessert:name', 'Ice Cream'); | |
nconf.set('dessert:flavor', 'chocolate'); | |
console.log(nconf.get('dessert')); | |
nconf.save(function (err) { | |
if (err) { | |
console.error(err.message); | |
return; | |
} | |
console.log('Configuration saved successfully.'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment