forked from ripple/explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (39 loc) · 1.37 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
35
36
37
38
39
40
41
42
43
44
45
46
require('dotenv').config()
const path = require('path')
const express = require('express')
const bodyParser = require('body-parser')
const compression = require('compression')
const routes = require('./routes/v1')
const log = require('./lib/logger')({ name: 'server' })
const PORT = process.env.PORT || 5001
const ADDR = process.env.ADDR || 'localhost'
const app = express()
const cacheBustRegExp = /\.[0-9a-f]{20}\./
const files = express.static(path.join(__dirname, '/../build'), {
etag: true, // Just being explicit about the default.
lastModified: true, // Just being explicit about the default.
setHeaders: (res, filePath) => {
if (filePath.endsWith('.html')) {
// All the project's HTML files end in .html
res.setHeader('Cache-Control', 'no-cache')
} else if (cacheBustRegExp.test(filePath)) {
// If the RegExp matched, then we have a versioned URL.
res.setHeader('Cache-Control', 'max-age=31536000')
}
},
})
app.use(compression())
app.use(bodyParser.json())
app.use(files)
app.use('/api/v1', routes)
if (process.env.NODE_ENV === 'production') {
app.get('*', (_req, res) => {
res.sendFile(path.join(__dirname, '/../build/index.html'))
})
}
app.use('*', (req, res) => {
log.error('not found:', req.originalUrl)
res.status(404).send({ error: 'route not found' })
})
app.listen(PORT, ADDR)
log.info(`server listening on ${ADDR}:${PORT}`)