-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
34 lines (26 loc) · 1.01 KB
/
index.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
const express = require('express');
const next = require('next');
const path = require('path');
const api = require('./api');
const PORT = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
const nextApp = next({ dev });
const handleNextPages = nextApp.getRequestHandler();
nextApp.prepare().then(() => {
const expressServer = express();
/* Static assets are kept at /public folder */
expressServer.use(express.static(path.resolve(__dirname, '..', 'public')));
/* APIs go there */
expressServer.use('/api', api);
/* Clean URL for rendering Product Information*/
expressServer.get('/product/:id', (req, res) => {
nextApp.render(req, res, '/product', req.params);
});
/* All website routes are automatically handled by next */
expressServer.get('*', handleNextPages);
/* Listen to the server now */
expressServer.listen(PORT, err => {
if (err) throw err;
console.log(`> Ready on http://localhost:${PORT}`); // eslint-disable-line no-console
});
});