Files
hudozka/src/server.js
T
2019-08-01 17:10:50 +03:00

47 lines
1.1 KiB
JavaScript

const Koa = require('koa')
const next = require('next')
const { get } = require('koa-route')
const { connect } = require('./core/db')
const Page = require('./core/Page')
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const port = parseInt(process.env.PORT, 10) || 3000
app.prepare().then(async () => {
try {
await connect()
} catch (e) {
console.error(e.message)
process.exit(1)
}
const pages = await Page.find({})
const pagesUrls = pages.map(x => x.url)
const server = new Koa()
server.use(async (ctx, next) => {
ctx.res.statusCode = 200
await next()
})
server.use(get('*', async ctx => {
const url = ctx.req.url
if (pagesUrls.includes(url)) {
await app.render(ctx.req, ctx.res, '/next', ctx.query)
} else {
await handle(ctx.req, ctx.res)
}
ctx.respond = false
}))
server.listen(port, () => {
console.log(`> Ready on http://localhost:${port}`)
})
})