add an object stream example

This commit is contained in:
Jonathan Ong 2013-12-24 22:49:44 -08:00
parent 1ea6e020b5
commit 66059cff4e
3 changed files with 50 additions and 0 deletions

View File

@ -16,6 +16,7 @@
"co-views": "~0.1.0",
"ejs": "~0.8.5",
"swig": "~1.2.2",
"streaming-json-stringify": "~1.0.0",
"koa-logger": "~1.1.0",
"koa-route": "~1.0.2",
"koa-static": "~1.4.0"

30
stream-objects/app.js Normal file
View File

@ -0,0 +1,30 @@
var koa = require('koa');
var JSONStream = require('streaming-json-stringify');
var app = module.exports = koa();
app.use(function *(){
this.type = 'json';
var stream = this.body = JSONStream();
stream.on('error', this.onerror);
setTimeout(function(){
stream.write({
id: 1
});
}, 1);
setTimeout(function(){
stream.write({
id: 2
});
}, 2);
setTimeout(function(){
stream.end();
}, 3);
});
if (!module.parent) app.listen(3000);

19
stream-objects/test.js Normal file
View File

@ -0,0 +1,19 @@
var app = require('./app');
var request = require('supertest').agent(app.listen());
describe('Stream Objects', function(){
it('GET /', function(done){
request
.get('/app.js')
.expect(200, function(err, res){
if (err) return done(err);
res.body.should.eql([{
id: 1
}, {
id: 2
}]);
done();
});
});
});