toString: support rehydration

This commit is contained in:
Yoshua Wuyts
2016-05-24 02:10:17 +09:00
parent 80dca262ad
commit 60665f2a71
8 changed files with 159 additions and 33 deletions
+17 -6
View File
@@ -1,4 +1,7 @@
const serverRouter = require('server-router')
const hyperstream = require('hyperstream')
const browserify = require('browserify')
const bankai = require('bankai')
const http = require('http')
const PORT = 8080
@@ -17,18 +20,26 @@ server.listen(PORT, () => process.stdout.write(`listening on port ${PORT}\n`))
// null -> fn
function createRouter () {
const apiRouter = serverRouter('/404')
apiRouter.on('/404', (req, res) => res.end('not found'))
apiRouter.on('/', (req, res) => {
res.end(JSON.stringify({ routes: [ '/', '/404' ] }))
})
apiRouter.on('/404', (req, res) => res.end('404 not found'))
apiRouter.on('/', (req, res) => res.end('nothing to be found here'))
const js = bankai.js(browserify, require.resolve('./client.js'))
apiRouter.on('/bundle.js', (req, res) => js(req, res).pipe(res))
return apiRouter
}
// render the client to string
// based on the requested url
// (obj, obj) -> null
const createIndex = bankai.html({ favicon: false, css: false })
function handleHtml (req, res) {
const html = client.toString(req.url, { message: 'hello server!' })
res.setHeader('Content-Type', 'text/html; charset=utf-8')
res.end(html)
const state = { message: { server: 'hello server!' } }
const inner = client.toString(req.url, state)
const hs = hyperstream({ 'body': { _appendHtml: inner } })
createIndex(req, res).pipe(hs).pipe(res)
}