-
Notifications
You must be signed in to change notification settings - Fork 2
/
frontserver.real.js
executable file
·58 lines (46 loc) · 1.45 KB
/
frontserver.real.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
55
56
57
58
const opn = require('opn')
const path = require('path')
const express = require('express')
const fs = require('fs')
const webpack = require('webpack')
const webpackMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')
const config = require('./webpack.prod.config.js')
require('dotenv').config({ path: './config/real.env' })
const port = process.env.PORT || 18888
const app = express()
// this middleware serves all js files as gzip
app.use(function (req, res, next) {
const originalPath = req.path
if (!originalPath.startsWith('/bundle')) {
next()
return
}
try {
if (originalPath.endsWith('.css')) {
res.set('Content-Type', 'text/css')
}
res.append('Content-Encoding', 'gzip')
res.setHeader('Vary', 'Accept-Encoding')
res.setHeader('Cache-Control', 'public, max-age=512000')
req.url = `${req.url}.gz`
} catch (e) {
console.log(e)
}
next()
})
app.use(express.static(path.join(__dirname, 'dist/static')))
app.use(express.static(path.join(__dirname, 'dist')))
app.get('*', function response(req, res) {
res.sendFile(path.join(__dirname, 'dist/index.html'))
})
app.listen(port, '0.0.0.0', function onStart(err) {
if (err) {
console.log(err)
}
console.info('==> 🌎 Listening on port %s. Open up http://0.0.0.0:%s/ in your browser.', port, port)
opn(`http://localhost:${port}`)
.catch(err => {
console.log(`can't open in your pc`)
})
})