/
server.js
57 lines (49 loc) · 1.38 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/** @type {import('fastify').FastifyInstance} */
const fastify = require('fastify')({
logger: { level: 'error' },
pluginTimeout: 0,
})
const Next = require('next')
const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
fastify.register((fastify, opts, next) => {
const app = Next({ dev })
const handle = app.getRequestHandler()
app
.prepare()
.then(() => {
if (dev) {
fastify.get('/_next/*', (req, reply) => {
return handle(req.raw, reply.raw).then(() => {
reply.hijack()
})
})
}
fastify.get('/a', (req, reply) => {
return app.render(req.raw, reply.raw, '/a', req.query).then(() => {
reply.hijack()
})
})
fastify.get('/b', (req, reply) => {
return app.render(req.raw, reply.raw, '/b', req.query).then(() => {
reply.hijack()
})
})
fastify.all('/*', (req, reply) => {
return handle(req.raw, reply.raw).then(() => {
reply.hijack()
})
})
fastify.setNotFoundHandler((request, reply) => {
return app.render404(request.raw, reply.raw).then(() => {
reply.hijack()
})
})
next()
})
.catch((err) => next(err))
})
fastify.listen({ port }, (err) => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})