forked from JasonBoy/koa-web-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
176 lines (158 loc) · 4.27 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict';
const config = require('./config/env');
const path = require('path');
const Koa = require('koa');
const mount = require('koa-mount');
const compress = require('koa-compress');
const session = require('koa-session');
const serveStatic = require('koa-static');
const convert = require('koa-convert');
const helmet = require('koa-helmet');
const favicon = require('koa-favicon');
const isEmpty = require('lodash.isempty');
const { logger, Logger } = require('./services/logger');
const index = require('./routes/index');
const { handleApiRequests } = require('./routes/proxy');
const sysUtils = require('./config/utils');
// const isSSREnabled = config.isSSREnabled();
const PORT = config.getListeningPort();
const DEV_MODE = config.isDevMode();
const DEFAULT_PREFIX_KEY = 'defaultPrefix';
const API_ENDPOINTS = config.getApiEndPoints();
const isHMREnabled = config.isHMREnabled();
function initAppCommon() {
const app = new Koa();
app.env = config.getNodeEnv() || 'development';
app.keys = ['koa-web-kit'];
app.proxy = true;
app.use(Logger.createMorganLogger());
app.use(logger.createRequestsLogger());
app.use(helmet());
return app;
}
function initApp(app) {
if (!DEV_MODE) {
app.use(compress());
}
app.use(favicon(__dirname + 'build/app/favicon.ico'));
let staticPrefix = path.join(
config.getAppPrefix(),
config.getStaticPrefix() || '/'
);
if (sysUtils.isWindows()) {
staticPrefix = sysUtils.replaceBackwardSlash(staticPrefix);
}
app.use(
mount(
staticPrefix,
serveStatic(path.join(__dirname, 'build/app'), {
// one month cache for prod
maxage: DEV_MODE ? 0 : 2592000000,
gzip: false,
})
)
);
app.use(session(app));
app.use(index.routes());
app.on('error', err => {
logger.error(err.stack);
});
return app;
}
function listen(app, port = PORT) {
const server = app.listen(port, '0.0.0.0');
logger.info(`Koa listening on port ${port}`);
if (DEV_MODE) {
logger.info(`visit: http://localhost:${port}`);
}
return server;
}
//React SSR
async function initSSR() {}
async function initHMR(app) {
if (!isHMREnabled) return;
let HMRInitialized = false;
logger.info('HMR enabled, initializing HMR...');
const koaWebpack = require('koa-webpack');
const historyApiFallback = require('koa-history-api-fallback');
const webpack = require('webpack');
const webpackConfig = require('./config/webpack.config.dev');
const compiler = webpack(
Object.assign({}, webpackConfig, {
stats: {
modules: false,
colors: true,
},
})
);
return new Promise((resolve, reject) => {
koaWebpack({
compiler,
hotClient: {
port: 0,
logLevel: 'error',
hmr: true,
reload: true,
},
devMiddleware: {
index: 'index.html',
publicPath: webpackConfig.output.publicPath,
watchOptions: {
aggregateTimeout: 0,
},
writeToDisk: true,
stats: {
modules: false,
colors: true,
children: false,
},
},
})
.then(middleware => {
if (!HMRInitialized) {
HMRInitialized = true;
app.use(convert(historyApiFallback()));
app.use(middleware);
middleware.devMiddleware.waitUntilValid(resolve);
}
})
.catch(err => {
logger.error('[koa-webpack]:', err);
reject();
});
});
}
function initProxy(app) {
//api proxy
if (config.isNodeProxyEnabled() && !isEmpty(API_ENDPOINTS)) {
for (const prefix in API_ENDPOINTS) {
if (
API_ENDPOINTS.hasOwnProperty(prefix) &&
prefix !== DEFAULT_PREFIX_KEY
) {
let endPoint = API_ENDPOINTS[prefix];
if ('string' !== typeof endPoint) {
endPoint = endPoint.endpoint;
}
app.use(handleApiRequests(prefix, endPoint));
logger.info('Node proxy[' + endPoint + '] enabled for path: ' + prefix);
}
}
}
}
module.exports = {
listen,
/**
*
* @return {Promise<Koa>}
*/
create: async function() {
const app = initAppCommon();
initProxy(app);
await initSSR();
await initHMR(app);
initApp(app);
return Promise.resolve(app);
// logger.info(`${isHMREnabled ? 'HMR & ' : ''}Koa App initialized!`);
},
};