This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (41 loc) · 1.49 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
54
const express = require('express')
const bodyParser = require('body-parser')
const html = require('./page')
const app = express()
app.use(bodyParser.json())
app.get('/health', (req, res) => res.json({ ok: true }))
app.use('/services', require('./services/api'))
app.use('/import', require('./services/import'))
app.use('/font-awesome', express.static('node_modules/font-awesome'))
app.use('/chartist', express.static('node_modules/chartist'))
if (process.env.NODE_ENV !== 'production') {
console.log('DEV mode')
const webpack = require('webpack')
const devMiddleware = require('webpack-dev-middleware')
const middlewareOptions = {
stats: {
colors: true
},
noInfo: true
}
app.use(devMiddleware(webpack(require('./webpack.dev.config')), middlewareOptions))
app.use(require('less-middleware')('src', { dest: 'public' }))
}
app.use('/', express.static('public'))
const maybeUpgradeToHttps = ({ headers, url }, res, next) => {
const protocol = headers['x-forwarded-proto']
if (protocol && protocol !== 'https') {
res.status(307).set({ Location: `https://${headers.host}${url}` }).end()
} else {
next()
}
}
app.get('/*', maybeUpgradeToHttps, (req, res) => res.send(html))
const port = parseInt(process.env.PORT, 10) || 3000
// error handler
// noinspection JSUnusedLocalSymbols
app.use('/', (error, req, res, next) => {
if (error.stack) console.error(error.stack)
res.status(500).json(error)
})
app.listen(port, () => console.log(`running: http://localhost:${port}`))