Last active
August 29, 2015 14:26
-
-
Save calebmer/1e087bc11da0ebdf38f6 to your computer and use it in GitHub Desktop.
The future of web apps in node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Next generation of building HTTP apps in NodeJS. Implements the HTML | |
* streaming API through ES6 generators. ES7 async functions are used to fetch | |
* data. | |
* | |
* Why is this a good idea? | |
* 1) It is beautiful. You setup a consistent API backend and your frontend | |
* backend (tongue twister) just looks like this. Layout implementations | |
* would be a bit odd, but it harkens back to plain old PHP 'require's. | |
* 2) Most modern, ES6, ES7, streams. All the new pretties. | |
* 3) It is known that using the HTML streaming API *will dramatically* speed up | |
* percieved page load. | |
*/ | |
import Template from ...; | |
import Api from ...; | |
import Page from ...; | |
import App from ...; | |
App.get('/timeline', async function (req, res) { | |
// Stream won't end until `page.end()` is called, so we can feed it to `res` here | |
let page = new Page(); | |
res(page); | |
page.add('head'); | |
let currentUser = await Api.get('/user'); | |
page.add('navbar', {currentUser}); | |
// Notice the `.stream()`, fancy :) | |
let tweets = Api.get('/tweets').stream(); | |
page.add('tweet', tweets); | |
page.add('footer'); | |
page.add('foot'); | |
page.end(); | |
}); | |
// Another example implementation using generators | |
App.get('/timeline', async function (req, res) { | |
let page = new Page(); | |
res(page); | |
let currentUser = await Api.get('/user'); | |
let tweets = await Api.get('/tweets'); | |
page.add(function* () { | |
yield Template.compile('head', { title: 'Timeline' }); | |
yield Template.compile('navbar', {currentUser}); | |
for (let if = 0, max = tweets.length; i < max; i++) { | |
yield Template.compile('tweet', tweets[i]); | |
} | |
yield Template.compile('footer', {}); | |
yield Template.compile('foot', {}); | |
}); | |
page.end(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment