2018-07-19 19:27:22 +00:00
|
|
|
const fastify = require('fastify')({ logger: { level: 'error' } })
|
2017-10-03 05:58:26 +00:00
|
|
|
const Next = require('next')
|
|
|
|
|
|
|
|
const port = parseInt(process.env.PORT, 10) || 3000
|
|
|
|
const dev = process.env.NODE_ENV !== 'production'
|
|
|
|
|
2018-07-19 19:27:22 +00:00
|
|
|
fastify.register((fastify, opts, next) => {
|
|
|
|
const app = Next({ dev })
|
2018-12-17 16:34:32 +00:00
|
|
|
app
|
|
|
|
.prepare()
|
2018-07-19 19:27:22 +00:00
|
|
|
.then(() => {
|
|
|
|
if (dev) {
|
|
|
|
fastify.get('/_next/*', (req, reply) => {
|
2018-12-17 16:34:32 +00:00
|
|
|
return app.handleRequest(req.req, reply.res).then(() => {
|
|
|
|
reply.sent = true
|
|
|
|
})
|
2018-07-19 19:27:22 +00:00
|
|
|
})
|
|
|
|
}
|
2017-10-03 05:58:26 +00:00
|
|
|
|
2018-07-19 19:27:22 +00:00
|
|
|
fastify.get('/a', (req, reply) => {
|
2018-12-17 16:34:32 +00:00
|
|
|
return app.render(req.req, reply.res, '/b', req.query).then(() => {
|
|
|
|
reply.sent = true
|
|
|
|
})
|
2018-07-19 19:27:22 +00:00
|
|
|
})
|
2017-10-03 05:58:26 +00:00
|
|
|
|
2018-07-19 19:27:22 +00:00
|
|
|
fastify.get('/b', (req, reply) => {
|
2018-12-17 16:34:32 +00:00
|
|
|
return app.render(req.req, reply.res, '/a', req.query).then(() => {
|
|
|
|
reply.sent = true
|
|
|
|
})
|
2018-07-19 19:27:22 +00:00
|
|
|
})
|
2017-10-03 05:58:26 +00:00
|
|
|
|
2018-07-19 19:27:22 +00:00
|
|
|
fastify.get('/*', (req, reply) => {
|
2018-12-17 16:34:32 +00:00
|
|
|
return app.handleRequest(req.req, reply.res).then(() => {
|
|
|
|
reply.sent = true
|
|
|
|
})
|
2018-07-19 19:27:22 +00:00
|
|
|
})
|
2017-10-03 05:58:26 +00:00
|
|
|
|
2018-07-19 19:27:22 +00:00
|
|
|
fastify.setNotFoundHandler((request, reply) => {
|
2018-12-17 16:34:32 +00:00
|
|
|
return app.render404(request.req, reply.res).then(() => {
|
|
|
|
reply.sent = true
|
|
|
|
})
|
2018-07-19 19:27:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
next()
|
2018-03-27 18:11:03 +00:00
|
|
|
})
|
2018-12-17 16:34:32 +00:00
|
|
|
.catch(err => next(err))
|
2018-07-19 19:27:22 +00:00
|
|
|
})
|
|
|
|
|
2018-12-17 16:34:32 +00:00
|
|
|
fastify.listen(port, err => {
|
2018-07-19 19:27:22 +00:00
|
|
|
if (err) throw err
|
|
|
|
console.log(`> Ready on http://localhost:${port}`)
|
|
|
|
})
|