-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
58 lines (46 loc) · 1.26 KB
/
server.ts
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
58
import express from 'express'
import next from 'next'
import logger from './logger'
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const PORT = process.env.CANADAGRAPHS_PORT || 3000
const cubeCache = {}
process.on('unhandledRejection', event => {
logger.error(event)
})
// A valid cube ID is a 8-numbers long string
function isValidCubeId(cubeId: string) {
return /[0-9]{8}/.test(cubeId)
}
app
.prepare()
.then(async () => {
const server = express()
// if (process.env.NODE_ENV === 'production') server.use(expressLogger)
// server.use((req, res, next) => {
// logger.info(req.url, req.headers['user-agent']);
// next();
// });
server.get('/chart/:cubeId', async (req, res) => {
const { cubeId } = req.params
if (isValidCubeId(cubeId) === false) {
res.status(400).send('Invalid cubeId')
return
}
app.render(req, res, '/chart', {
cubeId,
})
})
server.get('*', (req, res) => {
return handle(req, res)
})
server.listen(PORT, err => {
if (err) throw err
logger.info(`> Ready on http://localhost:${PORT}`)
})
})
.catch(ex => {
logger.error(ex.stack)
process.exit(1)
})