-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
132 lines (119 loc) · 3.38 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'use strict';
const Koa = require('koa');
const App = new Koa();
const path = require('path');
const bodyParser = require('koa-bodyparser');
// const views = require('koa-views');
const koaNunjucks = require('koa-nunjucks-2');
const convert = require('koa-convert');
const serverStatic = require('koa-static');
const logger = require('koa-logger');
const compose = require('koa-compose');
const compress = require('koa-compress');
const helmet = require('koa-helmet');
const validate = require('koa-validate');
const session = require('koa-session');
const conditional = require('koa-conditional-get');
const etag = require('koa-etag');
const webpack = require('webpack');
const devMiddleware = require('koa-webpack-dev-middleware');
const hotMiddleware = require('koa-webpack-hot-middleware');
const notFound = require('./server/middleware/404');
const error = require('./server/middleware/error');
const serverId = require('./server/middleware/serverId');
const router = require('./server/router/router');
const util = require('./util');
const webpackConfig = require('./webpack.config.js');
const compiler = webpack(webpackConfig);
/**
* Koa2 config
*/
App.name = 'Koa2-Demo';
/**
* Middlewares
*/
if (util.isDEV || util.isLOCAL) {
/* Koa logger midileware | Near the top of all middleware */
App.use(logger());
/**
* Hot Module Reload Middleware for Koa2
* dev-middleware监听变动,重新编译
*/
App.use(convert(devMiddleware(compiler, {
noInfo: false,
quiet: false,
watchOptions: {
aggregateTimeout: 300,
poll: true
},
publicPath: util.getPublicPath(),
stats: {
colors: true
}
})));
/**
* hot-middleware监听编译事件,
* 把变动通知到每一个通过Server Sent Events 连接的客户端,客户端接收到消息,
* 检查本地是否是新的,如果过期了触发webpack的热更新
*/
App.use(convert(hotMiddleware(compiler)));
}
/* Koa static (用来处理非路由访问的文件) */
App.use(serverStatic(path.join(__dirname, '/static')));
/**
* Bug: can not use extends, err info: can not found _layout.html
* so, use koa-nunjucks-2 instead!
*/
/** App.use(views(__dirname + '/server/views', {
* map: { html: 'nunjucks' }
* cache: util.isDEV || util.isLOCAL ? false : 'memory'
* }));
*/
/* Template Engine -- nunjunks */
App.use(koaNunjucks({
ext: 'html',
path: path.join(__dirname, '/server/views'),
nunjucksConfig: {
autoescape: true,
noCache: util.isDEV || util.isLOCAL
}
}));
/* A body parser for koa */
//App.use(bodyParser());
/**
* Add Header
*/
App.use(serverId());
/**
* koa-helmet is a wrapper for helmet to work with koa.
* It provides important security headers to make your app more secure by default.
*/
App.use(helmet());
/**
* Compress middleware for Koa
*/
App.use(compress());
/**
* Etag support
*/
App.use(conditional());
App.use(etag());
/**
* Session Middleware for Koa
*/
App.use(session(App));
/* 404 Error Handler */
App.use(notFound());
/* 500 Error Handler */
App.use(error());
/**
* Router
*/
router.init(App);
/**
* Error Handler
*/
App.on('error', (err, ctx) => {
console.log(`Server Error: ${err}`);
});
module.exports = App;