Created
July 1, 2013 02:20
-
-
Save bingeboy/5897969 to your computer and use it in GitHub Desktop.
Node Streams from @subsacks video here: http://www.youtube.com/watch?v=lQAV3bPOYHo&feature=c4-overview-vl&list=PL818E3E69FFE55295
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
var request = require('request') | |
, JSONStream = require('JSONStream') | |
, es = require('event-stream') | |
var parser = JSONStream.parse(['rows', true, 'value') //emit parts that match this path (any element of the rows array) ---- Try added true after value | |
, req = request({url: 'http://isaacs.couchone.com/registry/_all_docs'}) | |
, logger = es.mapSync(function (data) { //create a stream that logs to stderr, | |
console.error(data) | |
return data | |
}) | |
req.pipe(parser) | |
parser.pipe(logger) |
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
var http = require('http'); | |
var fs = require('fs'); | |
/* this example has a race condition... see pause-stream if you need to wait on a callback. */ | |
var server = http.createServer( function(req, res) { | |
var id = Math.random().toString(16).slice(2); | |
var dir = __dirname + '/tmp/' +id; | |
fs.mkdir(dir, function(err){ | |
console.log(err); | |
var ws = fs.createWriteStream(dir + '/output.txt'); | |
req.pipe(ws); //use ps.pip(ws) | |
req.on('end', function () { | |
res.end(id + '\n'); | |
}); | |
//resume the stream here with: ps.resume(); | |
}); | |
}); | |
server.listen(8080); | |
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
var fs = require('fs'); | |
var rs = fs.createReadStream('PATHTOFILEHERE'); | |
rs.pipe(process.stdout, { end : false }); |
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
var fs = require('fs'); | |
var Stream = require('stream'); | |
var ts = new Stream; | |
ts.readable = true; | |
ts.writeable = true; | |
ts.write = function(buffer){ | |
ts.emit('data', buffer.toString().toUpperCase()); | |
} | |
ts.end = function(buffer){ | |
if(arguments.length) ts.write(buffer); | |
ts.emit('end'); | |
} | |
process.stdin.pipe(ts).pipe(process.stdout); |
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
var fs = require('fs'); | |
var Stream = require('stream'); | |
var rs = new Stream; | |
rs.readable = true; | |
var counter = 0; | |
//write for infinity to console. | |
setInterval(function(){ | |
rs.emit('data', String(counter++)); | |
}, 10); | |
// emit end event after 5000ms | |
setTimeout(function() { | |
rs.emit('end'); | |
console.log(' ~~~Time Expired!'); | |
}, 5000); | |
rs.pipe(process.stdout, {end : false}); |
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
var fs = require('fs'); | |
var ws = fs.createWriteStream('./data/outputFromWS.txt'); | |
ws.write('bingeboy'); | |
ws.write('was\n'); | |
ws.write('here!'); | |
ws.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment