-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
59 lines (45 loc) · 1.29 KB
/
app.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
/**
* @Author: wangxu <ceekey>
* @Date: 2017-04-20 19:01:41
* @Email: [email protected]
* @Project: terra
* @Filename: app.js
* @Last modified by: ceekey
* @Last modified time: 2017-05-05 13:54:21
*/
'use strict';
let koa = require('koa');
let app = koa();
let router = require('./routes/router').router;
let logger = require('koa-logger')
let koaBody = require('koa-better-body');
//支持x-www-form-urlencoded
//支持form-data
app.use(koaBody());
//webpack hot dev server
let webpack = require('webpack');
let webpackDevMiddleware = require('koa-webpack-dev-middleware');
let webpackHotMiddleware = require('koa-webpack-hot-middleware');
let config = require('./webpack.config')
let compiler = webpack(config);
let getApi = require('./middlewares/index').getApi;
let staticServer = require('koa-static');
app.use(staticServer(__dirname + '/lib'));
app.use(getApi);
// use webpack
app.use(webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}));
app.use(webpackHotMiddleware(compiler));
// time
app.use(function * (next) {
let start = new Date;
yield next;
let ms = new Date - start;
this.set('X-Response-Time', ms + 'ms');
});
app.use(logger());
app.use(router.routes()).use(router.allowedMethods());
//listen
app.listen(3000);