Created
January 11, 2017 09:27
-
-
Save HendrikRoth/37da36c5fe6cae51c3d44d1ba5eb3e53 to your computer and use it in GitHub Desktop.
mithril-node-render async rewrite isomorphic app example.
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
import * as es6Promise from 'es6-promise' | |
es6Promise.polyfill() | |
import 'isomorphic-fetch' | |
import * as m from 'mithril' | |
export function oninit (vnode) { | |
return new Promise((resolve, reject) => { | |
vnode.attrs.title = 'Title!' | |
vnode.attrs.keywords = 'Mithril Node Render Server Render Example' | |
vnode.attrs.description = 'This is a mithril node render server rendering example.' | |
resolve() | |
}) | |
} | |
export function view (vnode) { | |
return m('h1', 'It works!') | |
} |
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
import * as view from './public-view' | |
export default { | |
'/': view, | |
'/:level1': view, | |
'/:level1/:level2': view | |
} |
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
import * as express from 'express' | |
import * as m from 'mithril' | |
import * as stream from 'mithril/stream' | |
import * as MithrilNodeRender from 'mithril-node-render' | |
const app = express() | |
const System = require('systemjs') | |
global.m = m | |
global.m.prop = stream | |
export function render(opts) { | |
const config = opts || {} | |
const { routes = {}, head = [] } = config | |
const base = (content, attrs = {}) => { | |
return [ | |
'<!doctype html>', | |
'<html>', | |
'<head>', | |
'<meta charset="utf-8">', | |
`<title>${attrs.title || config.appName}</title>`, | |
`<meta name="keywords" content="${attrs.keywords || ''}">`, | |
`<meta name="description" content="${attrs.description || ''}">`, | |
'</head>', | |
'<body>', | |
content, | |
'</body>', | |
'</html>' | |
].join('\n') | |
} | |
Object.keys(routes).forEach(route => { | |
const mod = routes[route] | |
app.get(route, (req, res, next) => { | |
res.type('html') | |
if (req.session && req.session.user) { | |
return res.end((base(''))) | |
} | |
const attrs = { | |
params: req.params, | |
query: req.query, | |
protocol: req.protocol, | |
host: req.get('host'), | |
title: '' | |
} | |
return MithrilNodeRender(mod, attrs) | |
.then(x => { | |
return res.end(base(x, attrs)) | |
}) | |
.catch(error => { | |
return res.end(error) | |
}) | |
}) | |
}) | |
return app | |
} |
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
const mock = require('mithril/test-utils/browserMock')() | |
global.window = mock | |
global.server = true | |
import * as express from 'express' | |
import * as bodyParser from 'body-parser' | |
import publicRoutes from './client/public' | |
import { render } from './render' | |
const app = express() | |
app.use(bodyParser.urlencoded({ | |
extended: true | |
})) | |
app.use(bodyParser.json()) | |
app.use(render({ | |
routes: publicRoutes | |
})) | |
app.listen(3305) | |
console.log('App running on ', 3305) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment