-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpress.js
38 lines (32 loc) · 1.12 KB
/
express.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
const express = require('express')
const compression = require('compression')
const path = require('path')
const favicon = require('serve-favicon')
const logger = require('morgan')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const port = process.env.PORT || 3000
const app = express()
const serverRoutes = require('./server/routes/server')
app.use(compression())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(__dirname + '/client/build'))
app.get('/server', serverRoutes)
// Static.
app.use((req, res, next) => {
if (req.url.match(/((scripts|styles|images|assets)\/\w*\.(svg|js|html|css|png|jpg|jpeg))/)) {
const match = req.url.match(/((scripts|styles|images|assets)\/\w*\.(svg|js|html|css|png|jpg|jpeg))/)[0]
console.log(`File requested: ${match}`)
res.sendFile(`public/${match}`, {root: './'})
return;
}
next()
})
app.get('*', (req, res) => {
res.sendFile('/client/src/index.html', { root: __dirname })
})
app.listen(port, () => {
console.log(`Listening on http://localhost:${port}.`)
})