BigPipe using koa and component
Given a subreddit, this app does the following:
- An image grid based on horizontal-grid-packing of the latest imgur posts.
- Show you the title of the subreddit and the number of subscribers.
- Show you the top 5 posts within the past day, week, month, year, and all time.
In total, that is 7 asynchronous calls with the request to imgur being the slowest.
This example requires node v0.11.3+ for generator support. Here's an example installation:
# Install and use node v0.11nvm install 0.11nvm use 0.11# Clone this examplegit clone git://github.com/jonathanong/bigpipe-examplecd bigpipe-example# Install all dependencies and build the assetsnpm install# Start the server. Serves on port 3456 by defaultnpm start
This example implements 3 strategies. The strategies are listed from slowest to fastest and worst to best. This app is designed to be able to switch between any of these 3 modes with minimal code.
These strategies are all implemented as streams. If you want to implement caching, you would want to buffer the result and process it before sending it to the client. Fortunately, this is easy with koa.
All asynchronous functions are blocking and are called within the template. This is the easiest way to build a webpage and is similar traditional web languages like PHP, Ruby, and Python. This is actually difficult to do in vanilla node.js, but very easy with koa.
All asynchronous functions are executed before the rendering of the template.
This is similar to traditional Express apps where rendering occurs at
res.render() (assuming the developer executes the functions in parallel).
This is the ideal method if you intend to cache the page.
All asynchronous functions are executed after the initial layout is flushed to the client so the client will see a barebone layout immediately. Each asynchronous function, also called a pagelet, is rendered and pushed independently to the client. Each pagelet is generally self-contained, meaning it has its own HTML, JS, CSS.
Koa is a new generator-based framework based on co and is the spiritual successor to express. By using generators for control flow, there's no more callback hell or painful error handling. Look at the source code to see for yourself.
Component is a CommonJS-based client-side package manager and build system.
Not only does it allow you to easily consume 3rd party components (packages of JS, CSS, HTML, images, fonts, etc.),
it also allows you to organize your app as independent components.
/templates folders where finding what you need is difficult.
View the /client to see how apps should be structured.
- client/ - all client-side CSS and JS that is not within a
- routes/ - all routes.
- view/ - base constructor which all
Views inherit from.
- views/ - all the different views.
The MIT License (MIT)
Copyright (c) 2013 Jonathan Ong firstname.lastname@example.org
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.