-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
42 lines (34 loc) · 1.08 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
const path = require('path')
const fs = require('fs')
const compression = require('compression')
const express = require('express')
const yaml = require('js-yaml')
const app = express()
const host = process.env.HOST || '0.0.0.0'
const port = process.env.PORT || 3000
const redirects = yaml.safeLoad(fs.readFileSync('./data/redirects.yml', 'utf8'))
app.use(compression({ threshold: 0 })) // gzip
// serve up the generated static pages
app.use(express.static('dist/html', { extensions: ['html'], index: '_index.html' }))
// serve up static assets
app.use('/', express.static('assets'))
// example custom route
app.get('/special', (req, res) => {
res.json({some: 'json stuff'})
})
// 301 redirects for eg SEO
app.use((req, res, next) => {
const path = req.path
if (redirects[path]) {
console.log(`301 redirect: ${path} -> ${redirects[path]}`)
res.redirect(301, redirects[path])
} else {
next()
}
})
// 404s
app.use((req, res) => {
res.status(404).sendFile(path.join(__dirname, '/dist/html/404.html'))
})
app.listen(port, host)
console.log(`Server listening on ${host}:${port}`)