Skip to content

Instantly share code, notes, and snippets.

@cavedave
Last active January 1, 2016 15:19
Show Gist options
  • Save cavedave/8163037 to your computer and use it in GitHub Desktop.
Save cavedave/8163037 to your computer and use it in GitHub Desktop.
Node stream adventure code
/*
Part 1 Stream adventure
*/
console.log("beep boop");
/*
Part 2 Meet Pipe
*/
var fs = require('fs');
fs.createReadStream(process.argv[2]).pipe(process.stdout);
/*
Part 3 Input Output
*/
process.stdin.pipe(process.stdout);
/*
Part 4 Transform
*/
var through = require('through');
var tr = through(write);
process.stdin.pipe(tr).pipe(process.stdout);
function write (buf) { this.queue(buf.toString().toUpperCase());}
/*
Part 4 Lines upper and lower
*/
var through = require('through');
var split = require('split');
var tr = through(write);
var linenum = 0;
var write = function (buf){
var strbuf = buf.toString();
this.queue(linenum % 2 === 0
? strbuf.toLowerCase() + '\n'
: strbuf.toUpperCase() + '\n'
);
linenum ++;
};
var tr = through(write);
process.stdin.pipe(split()).pipe(tr).pipe(process.stdout);
/*
Part 5 Concat and reverse
*/
/*
part 6 http server
*/
var http = require('http');
var through = require('through');
var server = http.createServer(function (req, res) {
if (req.method === 'POST') {
req.pipe(through(function (buf) {
this.queue(buf.toString().toUpperCase());
})).pipe(res);
}
else res.end('send me a POST\n');
});
server.listen(parseInt(process.argv[2]));
/*
part 7
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment