models: fix state namespace
This commit is contained in:
+23
-11
@@ -2,21 +2,33 @@ const serverRouter = require('server-router')
|
||||
const http = require('http')
|
||||
|
||||
const PORT = 8080
|
||||
|
||||
const client = require('./client')
|
||||
|
||||
const server = http.createServer(createRouter())
|
||||
server.listen(PORT, () => console.log(`listening on port ${PORT}`))
|
||||
// If an incoming request accepts "text/html", render the
|
||||
// appropriate HTML. Else use the API server
|
||||
const apiRouter = createRouter()
|
||||
const server = http.createServer(function (req, res) {
|
||||
if (/text\/html/.test(req.headers.accept)) handleHtml(req, res)
|
||||
else apiRouter(req, res)
|
||||
})
|
||||
server.listen(PORT, () => process.stdout.write(`listening on port ${PORT}\n`))
|
||||
|
||||
// create a new router
|
||||
// null -> fn
|
||||
function createRouter () {
|
||||
const router = serverRouter('/404')
|
||||
|
||||
router.on('/404', (req, res) => res.end('not found'))
|
||||
router.on('/', function (req, res, params) {
|
||||
const html = client.toString('/', { message: 'hello server!' })
|
||||
res.setHeader('Content-Type', 'text/html; charset=utf-8')
|
||||
res.end(html)
|
||||
const apiRouter = serverRouter('/404')
|
||||
apiRouter.on('/404', (req, res) => res.end('not found'))
|
||||
apiRouter.on('/', (req, res) => {
|
||||
res.end(JSON.stringify({ routes: [ '/', '/404' ] }))
|
||||
})
|
||||
return apiRouter
|
||||
}
|
||||
|
||||
return router
|
||||
// render the client to string
|
||||
// based on the requested url
|
||||
// (obj, obj) -> null
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user