openscihub.org: manage web pages isomorphically
npm install osh-pagesA framework for building isomorphic web apps that are wrappers around resource
APIs. Consider this the controller layer, gluing APIs (the model layer) to
the display (view layer) with a keen eye on the holy grail.
Build 'em like they used to, have 'em work like they should.
What you have to do:
- Write isomorphic interfaces to APIs (this is often accomplished via an
isomorphic HTTP request
library...ahem...SuperAgent)
- Find or build a html/DOM rendering and diffing
library...ahem...ReactJS
- Hook them together using Pages.
What you get:
- Server-side rendering/handling of all GET/POST actions.
- Snappy initial page loads.
- AJAX navigation and submission without touching an
onclick or onsubmit (that's isomorphic).
- Built-in js module bundling and loading (via
dynapack).
```
npm install osh-pages
Consider the following files
``
server.js
routes.js
view-user.js
The contents of each are printed below; let's start with routes.js
as it should look the most familiar:
`js
module.exports = {
'view-user': {
path: '/users/
params: {
username: /^[a-z]+$/
}
}
// other routes here...
};
`
Each entry in the route map is a config object for an
osh-route.
The module, view-user.js exports a page prototype, which defines
the lifecycle methods for a page.
`js
var request = require('superagent'); // isomorphic!..mostly
module.exports = {
/**
* Read data from various APIs. In this case, we
* pull some user data from a fictitious API.
*/
read: function(pages, done) {
var page = this;
request.get('https://api.mysite.com/users/' + this.props.username)
.end(function(res) {
page.setState({
fullname: (
res.ok ?
res.body.fullname :
'Unknown user'
),
title: (
res.ok ?
this.props.username :
'Not found'
)
});
done();
});
},
/**
* Not a lifecycle method; separated from renderToString and
* renderToDocument for code reuse.
*/
renderBody: function() {
return (
'
/**
* Lifecycle method. Render on the server.
*/
renderToString: function() {
return (
'' +
'