2017-01-17 07:23:06 +00:00
|
|
|
const Koa = require('koa')
|
|
|
|
const next = require('next')
|
|
|
|
const Router = require('koa-router')
|
|
|
|
|
|
|
|
const dev = process.env.NODE_ENV !== 'production'
|
|
|
|
const app = next({ dev })
|
|
|
|
const handle = app.getRequestHandler()
|
|
|
|
|
|
|
|
app.prepare()
|
|
|
|
.then(() => {
|
|
|
|
const server = new Koa()
|
|
|
|
const router = new Router()
|
|
|
|
|
|
|
|
router.get('/a', function *() {
|
|
|
|
app.render(this.req, this.res, '/b', this.query)
|
|
|
|
this.respond = false
|
|
|
|
})
|
|
|
|
|
|
|
|
router.get('/b', function *() {
|
|
|
|
app.render(this.req, this.res, '/a', this.query)
|
|
|
|
this.respond = false
|
|
|
|
})
|
|
|
|
|
|
|
|
router.get('*', function *() {
|
|
|
|
handle(this.req, this.res)
|
|
|
|
this.respond = false
|
|
|
|
})
|
|
|
|
|
2017-01-18 03:45:29 +00:00
|
|
|
server.use(function *(next) {
|
|
|
|
// Koa doesn't seems to set the default statusCode.
|
|
|
|
// So, this middleware does that
|
|
|
|
this.res.statusCode = 200
|
|
|
|
yield next
|
|
|
|
})
|
|
|
|
|
2017-01-17 07:23:06 +00:00
|
|
|
server.use(router.routes())
|
|
|
|
server.listen(3000, (err) => {
|
|
|
|
if (err) throw err
|
|
|
|
console.log('> Ready on http://localhost:3000')
|
|
|
|
})
|
|
|
|
})
|