Last active
December 8, 2015 10:09
-
-
Save vermaslal/c9c966517a427c10f106 to your computer and use it in GitHub Desktop.
To update stream on middle using node js
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
'use strict'; | |
//Reference: https://www.safaribooksonline.com/blog/2013/05/01/using-streams-in-node-js/ | |
var fs = require('fs'); | |
var stream = require('stream'); | |
//var Transform = require('stream').Transform; | |
var mx = {}; | |
mx.bind_helo = '--------sham-----'; | |
var source = fs.createReadStream('/home/sarv/node_modules/smail/s.txt'); | |
var target = fs.createWriteStream('/home/sarv/node_modules/smail/replaced.txt'); | |
function createFile() { | |
var replaceSig = new stream.Transform({decodeStrings: false}); | |
replaceSig._transform = function (chunk, encoding, done) { | |
done(null, chunk.toString().replace('by sham', 'by ' + mx.bind_helo)); | |
}; | |
source.pipe(replaceSig).pipe(target); | |
} | |
createFile() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment