mirror of
https://github.com/koajs/examples.git
synced 2026-01-18 14:38:19 +00:00
35 lines
712 B
JavaScript
35 lines
712 B
JavaScript
'use strict';
|
|
|
|
const Readable = require('stream').Readable;
|
|
const co = require('co');
|
|
|
|
module.exports = class View extends Readable {
|
|
|
|
constructor(context) {
|
|
super();
|
|
|
|
// render the view on a different loop
|
|
co.call(this, this.render).catch(context.onerror);
|
|
}
|
|
|
|
_read() {}
|
|
|
|
*render() {
|
|
// push the <head> immediately
|
|
this.push('<!DOCTYPE html><html><head><title>Hello World</title></head>');
|
|
|
|
// render the <body> on the next tick
|
|
const body = yield done => {
|
|
setImmediate(() => done(null, '<p>Hello World</p>'));
|
|
};
|
|
|
|
this.push('<body>' + body + '</body>');
|
|
|
|
// close the document
|
|
this.push('</html>');
|
|
|
|
// end the stream
|
|
this.push(null);
|
|
};
|
|
};
|