mirror of
https://github.com/koajs/examples.git
synced 2026-01-18 14:38:19 +00:00
31 lines
522 B
JavaScript
31 lines
522 B
JavaScript
|
|
const Koa = require('koa');
|
|
const JSONStream = require('streaming-json-stringify');
|
|
|
|
const app = module.exports = new Koa();
|
|
|
|
app.use(async function(ctx) {
|
|
ctx.type = 'json';
|
|
const stream = ctx.body = JSONStream();
|
|
|
|
stream.on('error', ctx.onerror);
|
|
|
|
setImmediate(function() {
|
|
stream.write({
|
|
id: 1
|
|
});
|
|
|
|
setImmediate(function() {
|
|
stream.write({
|
|
id: 2
|
|
});
|
|
|
|
setImmediate(function() {
|
|
stream.end();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
if (!module.parent) app.listen(3000);
|