-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
53 lines (44 loc) · 1.3 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
const router = require('fastify')({ logger: { level: 'error' } });
const Next = require('next');
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
router.register((router, opts, next) => {
const app = Next({ dev });
app
.prepare()
.then(() => {
if (dev) {
router.get('/_next/*', (req, reply) =>
app.handleRequest(req.req, reply.res).then(() => {
reply.sent = true;
})
);
}
router.get('/item/:slug', (req, reply) =>
app.render(req.req, reply.res, '/item', req.params).then(() => {
reply.sent = true;
})
);
router.get('/category/:top/:sub', (req, reply) =>
app.render(req.req, reply.res, '/category', req.params).then(() => {
reply.sent = true;
})
);
router.get('/*', (req, reply) =>
app.handleRequest(req.req, reply.res).then(() => {
reply.sent = true;
})
);
router.setNotFoundHandler((request, reply) =>
app.render404(request.req, reply.res).then(() => {
reply.sent = true;
})
);
next();
})
.catch(err => next(err));
});
router.listen(port, err => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});