-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
66 lines (57 loc) · 1.86 KB
/
app.js
File metadata and controls
66 lines (57 loc) · 1.86 KB
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
59
60
61
62
63
64
const path = require('path')
const chalk = require('chalk')
const express = require('express')
const webpack = require('webpack')
const webpackDevServer = require('webpack-dev-server')
const gameServer = require('./game_server/start')
const routes = require('./routes')
const { port } = require('./config/index')
let app = express()
app.use((err, req, res, next) => {
res.status(err.status || 500)
res.send(err.stack || 'Service Error')
})
const createServer = () => {
app.use('/', routes)
app.listen(port, () => {
setTimeout(() => {
console.clear()
console.log('\n')
if (isDev) {
console.log(chalk.bold(chalk.white(`Github: ${chalk.gray(`https://github.com/j3812549/tiancai-mpa-ejs-hot-webpack-cli`)}`)))
console.log(chalk.bold(chalk.white(`Author: ${chalk.gray(`380012546@qq.com`)}`)))
}
console.log('\n')
console.log(
chalk.bold(chalk.green(`Loopback: ${chalk.blue(`http://localhost:${port}/`)}`))
)
new gameServer()
}, 1000)
})
}
const isDev = process.env.MODE_ENV === 'development'
if (isDev) {
// development
const webpackConfig = require(path.join(__dirname, './config/webpack.dev.config.js'))
const compiler = webpack(webpackConfig)
const server = new webpackDevServer({ hot: true, client: false }, compiler)
setTimeout(async () => {
await server.start()
createServer()
})
} else {
// production
const webpackConfig = require(path.join(__dirname, './config/webpack.prod.config.js'))
webpack(webpackConfig, (err, res) => {
if (err || res.hasErrors()) {
console.log("构建过程出错!")
} else {
console.log("构建成功!")
// 添加静态资源拦截转发
app.use(express.static(path.join(__dirname, 'dist')))
app.set('views', path.join(__dirname, 'dist'))
app.set('view engine', 'ejs')
createServer()
}
})
}