-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.dev.js
115 lines (99 loc) · 2.71 KB
/
index.dev.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* @file 开发环境主入口
* @author [email protected] (wangyisheng)
*/
const Koa = require('koa')
const Router = require('koa-router')
const glob = require('glob')
const koaWebpack = require('koa-webpack')
const static = require('koa-static')
const history = require('koa2-history-api-fallback')
const {PORT} = require('./config/server')
const {getRouterPath, log} = require('./utils/framework')
const webpackConfig = require('./vue/webpack.config.js')
const app = new Koa()
const router = new Router()
process.env.NODE_ENV = 'development'
registerApp()
async function registerApp () {
app.use(async (ctx, next) => {
log.info(ctx.url)
await next()
})
try {
// 所有 navigate 请求重定向到 '/'
// 因为 webpack-dev-server 只服务这个路由,否则就会漏到 node 端了。
app.use(history({
htmlAcceptHeaders: ['text/html'],
index: '/'
}))
app.use(static('public'))
await registerWebpack()
await registerMiddlewares()
await registerRoutes()
app.use(router.routes())
.use(router.allowedMethods())
.listen(PORT)
log.info('开发环境服务器启动于端口号', PORT, '等待 webpack 编译中,请稍候。\n\n')
} catch (e) {
log.error(e)
log.error('开发环境服务器启动失败\n\n')
}
}
async function registerRoutes () {
return new Promise((resolve, reject) => {
glob('actions/**/*.js', (err, files) => {
if (err) {
log.error('读取 actions 失败')
log.error(err)
reject()
return
}
files.forEach(actionPath => {
let action = require(`./${actionPath}`)
if (typeof action.handler !== 'function') {
log.warn(actionPath, '不是一个合法的 action,已经跳过')
return
}
if (!action.routerPath) {
action.routerPath = getRouterPath(actionPath)
}
router.get(action.routerPath, action.handler)
})
resolve()
})
})
}
async function registerMiddlewares () {
return new Promise((resolve, reject) => {
glob('middlewares/**/*.js', (err, files) => {
if (err) {
log.error('读取 middlewares 失败')
log.error(err)
reject()
return
}
files.forEach(middlewarePath => {
let middleware = require(`./${middlewarePath}`)
if (typeof middleware !== 'function') {
return
}
router.use(middleware)
})
resolve()
})
})
}
async function registerWebpack() {
return new Promise(resolve => {
koaWebpack({
config: webpackConfig,
devMiddleware: {
stats: 'minimal'
}
}).then(middleware => {
app.use(middleware)
resolve()
})
})
}