Last active
August 29, 2015 13:57
-
-
Save metafeather/9390671 to your computer and use it in GitHub Desktop.
Get, Parse and Transform JSONStreams on the fly before delivering to the browser
This file contains hidden or 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
// Get, Parse and Transform JSONStreams on the fly before delivering to the browser | |
// ref: http://nodestreams.com/ | |
var express = require('express'), | |
server = express(); | |
var fs = require("fs"), | |
JSONStream = require("JSONStream"), | |
es = require('event-stream'); | |
server.all( | |
'/my/json/api*', | |
function (req, res, next){ | |
// Stream output will be a string and you cannot pipe to express.json() so make sure the browser knows its JSON | |
res.setHeader('Content-Type', 'application/json; charset=utf-8'); | |
// Source can be a File or from a Request | |
var source = fs.createReadStream("data/huge_ammount_of.json"), | |
/* | |
JSONStream can parse the source in a number of formats, e.g. | |
1) {...} newline separated JSON Objects | |
2) [ | |
{...} array of JSON Objects | |
] | |
3) { | |
"some_props": "...", | |
"rows": [ | |
{...} nested array of JSON Objects in a wrapper | |
] | |
} | |
*/ | |
parser = JSONStream.parse("rows.*"), | |
// Convert a single JSON Object | |
convert = es.mapSync( | |
function(data) { | |
console.log('data received:', data); | |
// e.g. add a property | |
data.converted = true; | |
return data; | |
} | |
); | |
// Read file | |
source | |
// Parse JSON | |
.pipe(parser) | |
// convert individual JSON Object | |
.pipe(convert) | |
// convert JSON to strings in place | |
.pipe(JSONStream.stringify()) | |
// output Strings | |
.pipe(res); | |
} | |
); | |
server.listen('1337'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment