1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/custom-server-koa/server.js

42 lines
949 B
JavaScript
Raw Normal View History

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
})
server.use(function *(next) {
// Koa doesn't seems to set the default statusCode.
// So, this middleware does that
this.res.statusCode = 200
yield next
})
server.use(router.routes())
server.listen(3000, (err) => {
if (err) throw err
console.log('> Ready on http://localhost:3000')
})
})