models: fix state namespace

This commit is contained in:
Yoshua Wuyts
2016-05-23 14:40:52 +09:00
parent 0cc0caa8e1
commit dc840059a4
8 changed files with 40 additions and 24 deletions
+1 -2
View File
@@ -14,7 +14,7 @@ module.exports = {
}
}
function request (uri, send) {
function request (uri, send, state) {
http(uri, { json: true }, function (err, res, body) {
if (err) return send('app:error', { payload: 'HTTP error' })
if (res.statusCode !== 200) {
@@ -24,7 +24,6 @@ function request (uri, send) {
return send('app:error', { payload: message })
}
if (!body) {
console.log('req made!')
return send('app:error', { payload: 'fatal: no body received' })
}
send('api:set', { payload: body.message || body.title })
+1 -1
View File
@@ -34,7 +34,7 @@ module.exports = {
},
effects: {
error: function (action, state, send) {
const timeout = state.app.errorTimeDone - Date.now()
const timeout = state.errorTimeDone - Date.now()
setTimeout(function () {
send('app:error:delete')
}, timeout)
+1 -1
View File
@@ -6,7 +6,7 @@ const http = require('http')
const PORT = 8080
const server = http.createServer(createRouter())
server.listen(PORT, () => console.log(`listening on port ${PORT}`))
server.listen(PORT, () => process.stdout.write(`listening on port ${PORT}\n`))
var index = 0
const errors = [
+1 -1
View File
@@ -6,7 +6,7 @@ const http = require('http')
const PORT = 8080
const server = http.createServer(createRouter())
server.listen(PORT, () => console.log(`listening on port ${PORT}`))
server.listen(PORT, () => process.stdout.write(`listening on port ${PORT}\n`))
function createRouter () {
const router = serverRouter('/404')
+23 -11
View File
@@ -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)
}