-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
47 lines (42 loc) · 1.23 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
/**
* Server for Serving the Build Folder
* - enabling BrowserRouter with HsitoryFallback (all non existing routing to `/index.html`
* - beautify URL for `/` instead of `/index.html`
*/
import path from 'path'
import fs from 'fs'
import express from 'express'
import {fileURLToPath} from 'url'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const app = express()
const publicDir = path.resolve(__dirname, 'build')
// const historyFallback = 'index.html'
const historyFallback = false
app.use(function(req, res, next) {
if(req.path.indexOf('.') === -1) {
const file = publicDir + req.path + '.html'
fs.access(file, function(err) {
if(!err) {
req.url += '.html'
console.log(req.url, req.path)
}
next()
})
} else {
next()
}
})
app.use(express.static(publicDir))
/*
app.get('/*', (req, res) => {
if(historyFallback) {
res.sendFile(path.join(publicDir, 'index.html'))
} else {
// this is for react static 4040er
res.sendFile(path.join(publicDir, '404.html'))
}
})*/
app.listen(3030, () => {
console.log('Server started on port http://localhost:3030')
})