forked from koajs/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.js
More file actions
34 lines (26 loc) · 793 Bytes
/
view.js
File metadata and controls
34 lines (26 loc) · 793 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var Readable = require('stream').Readable;
var inherits = require('util').inherits;
var co = require('co');
module.exports = View
inherits(View, Readable);
function View(context) {
Readable.call(this, {});
// render the view on a different loop
co.call(this, this.render).catch(context.onerror);
}
View.prototype._read = function () {};
View.prototype.render = function* () {
// push the <head> immediately
this.push('<!DOCTYPE html><html><head><title>Hello World</title></head>');
// render the <body> on the next tick
var body = yield function (done) {
setImmediate(function () {
done(null, '<p>Hello World</p>');
});
};
this.push('<body>' + body + '</body>');
// close the document
this.push('</html>');
// end the stream
this.push(null);
};