This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (48 loc) · 1.25 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
'use strict';
const koa = require('koa');
const app = koa();
const serve = require('koa-static');
const Pug = require('koa-pug');
const config = require('./config');
const error = require('./lib/error');
const bodyParser = require('./lib/bodyParser');
const webpack = require('./lib/webpackMiddleware');
const apiRouter = require('./routes/api');
const pageRouter = require('./routes/page');
const logger = require('./lib/logger');
const assets = require('./assets/.assets.json');
// static serve
app.use(serve('assets'));
// webpack
webpack(app);
// logger
logger.register(app);
app.use(logger.useGlobalLogger());
// exception handler
app.use(error);
// view engine
const IS_DEVELOPMENT = app.env === 'development';
const defaultAssets = {
app: { js: '/script/bundle.js' },
vendor: { js: '/script/vendor.js' }
};
const pug = new Pug({
viewPath: './views',
basedir: './views',
noCache: IS_DEVELOPMENT,
debug: IS_DEVELOPMENT,
app: app,
locals: IS_DEVELOPMENT ? defaultAssets : assets
});
// bodyParser
app.use(bodyParser);
// router
app.use(apiRouter.routes());
app.use(pageRouter.routes());
if (require.main === module) {
// istanbul ignore next
// 开发&测试环境时覆盖
app.listen(config['app.port'] || 3000);
} else {
module.exports = app;
}