dongyu 50bcadf73e migrate to koa@2 (#108)
* support async/await

* change nodejs version 4 or 6 to 5.6

* eslint

* remove jade
2017-05-30 20:44:57 +08:00

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);
};
};