Skip to content

Instantly share code, notes, and snippets.

@gido
Created June 1, 2015 10:03
Show Gist options
  • Save gido/2995cfbee73ab9789cca to your computer and use it in GitHub Desktop.
Save gido/2995cfbee73ab9789cca to your computer and use it in GitHub Desktop.
Playing with Stream and Nodejs
var request = require("request");
var zlib = require("zlib");
var csv = require("csv-streamify");
var Handlebars = require("handlebars");
var es = require("event-stream");
var tmpl = Handlebars.compile("<li><a href="{{URL}}">{{Name}}</a> ({{City}})</li>");
// HTTP GET Request
request("http://nodestreams.com/input/people.csv.gz")
// Un-Gzip
.pipe(zlib.createGunzip())
// Parse CSV as Object
.pipe(csv({objectMode: true, columns: true}))
// Convert Object w/ Handlebars
.pipe(es.mapSync(tmpl))
// Join Strings
.pipe(es.join("\n"))
// Concat Strings
.pipe(es.wait())
// Wrap Strings
.pipe(es.mapSync(function(data) { return "<ul>" + data + "\n</ul>"; }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment