mirror of
https://github.com/koajs/examples.git
synced 2026-01-25 14:48:15 +00:00
add an object stream example
This commit is contained in:
parent
1ea6e020b5
commit
66059cff4e
@ -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
30
stream-objects/app.js
Normal 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
19
stream-objects/test.js
Normal 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();
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user