-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-dev-server.js
56 lines (45 loc) · 1.49 KB
/
client-dev-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
55
56
/* eslint-disable no-sync */
const app = require('express')()
const webpackConfig = require('./webpack.client.dev.js')
const webpack = require('webpack')
const webpackMiddleware = require('webpack-dev-middleware')
const hotMiddleware = require('webpack-hot-middleware')
const path = require('path')
const compiler = webpack(webpackConfig)
const webpackDevMiddleware = webpackMiddleware(compiler, {
noInfo: false,
publicPath: '/',
stats: {colors: true},
headers: {'Access-Control-Allow-Origin': '*'}
})
const mfs = webpackDevMiddleware.fileSystem
const fileRegExp = /[\w,\s-]+\.[A-Za-z0-9]+$/
app.use(webpackDevMiddleware)
app.use(hotMiddleware(compiler, {heartbeat: 500}))
// My own server-static implementation for memory-fs
app.get(fileRegExp, (req, res) => {
webpackDevMiddleware.waitUntilValid(() => {
const filePath = path.resolve(__dirname, './dist', req.url.slice(1))
let file = null
res.type(path.parse(req.url).base)
try {
file = mfs.readFileSync(filePath)
}
catch (e) {
return res.end()
}
res.end(file)
})
})
//TODO: Сделать полноценный сервер
app.get('*', (req, res) => {
webpackDevMiddleware.waitUntilValid(() => {
const indexFile = mfs
.readFileSync(path.resolve(__dirname, './dist/index.html'))
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Content-type', 'text/html')
res.send(indexFile)
})
})
app.listen(1488)
.on('connection', sock => sock.setNoDelay(true))