Skip to content

Instantly share code, notes, and snippets.

fs.appendFile('myfile.txt', 'this content goes at end of the file', function(err){
if(err) {
console.log('Error is thrown', err); throw err;
}
console.log('data is appended !!');
})
fs = require("fs");
path = "data/myfile.txt";
data = "This content is written from Node program";
result =fs.writeFileSync(path, data);
console.log('written contents are : ' + result);
fs = require("fs");
path = "myfile.txt";
data = "This content is written from Node program";
fs.writeFile(path, data, function(error){
if error
console.error("error: " + error.message);
else
console.log("Successful Write to " + path);
})
res= <destination file stream>; // It can be standard io, another file stream, etc
readStream = fs.createReadStream(filename);
readStream.on('open', function(){
readStream.pipe(res);
});
readStream.on('error', function(err){
res.end(err);
});
//read it synchronously
content= fs.readFileSync('package.json', 'utf-8');
console.log(content);
fs= require(‘fs’)
//read it asynchronously
fs.readFile('package.json', function(err, res){
if(err){
console.log('Error while reading the file content ');
}
console.log('file contents- ' + res)
})
@ajduke
ajduke / meteor-add.sh
Created October 17, 2015 15:00
Meteor - package
meteor add <package-name>
@ajduke
ajduke / output.json
Created April 11, 2015 11:11
JSON Gist
[
{
"raw": "This is some text to demonstrate how a recursive parser works it has ",
"data": "This is some text to demonstrate how a recursive parser works it has ",
"type": "text"
},
{
"raw": "b",
"data": "b",
"type": "tag",
@ajduke
ajduke / ErrorHandlerWrapper.js
Created April 10, 2015 05:54
Javascript Error handler
var fs = require('fs')
fs.readFile('app.js', errorHandler(function (content) {
console.log('File contents are ' + content)
})
)
function errorHandler(callback) {
return function (err, result) {
@ajduke
ajduke / s3.json
Created March 18, 2015 18:10
amazon-s3-part-1
{
"Version":"2012-10-17",
"Statement":[
{
"Sid":"AddPerm",
"Effect":"Allow",
"Principal": "*",
"Action":["s3:GetObject"],
"Resource":["arn:aws:s3:::ExampleBucket/*"]
}