-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathapp.js
225 lines (203 loc) · 5.72 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'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 helmet = require('koa-helmet');
const favicon = require('koa-favicon');
const isEmpty = require('lodash.isempty');
const conditional = require('koa-conditional-get');
const etag = require('koa-etag');
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();
const servingStaticIndex = config.isServingStaticIndex();
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());
app.use(helmet.xssFilter());
return app;
}
function initApp(app) {
app.use(conditional());
app.use(etag());
if (!DEV_MODE) {
app.use(compress());
}
app.use(favicon(path.join(__dirname, 'build/app/favicon.ico')));
// =====serve static=====
const ROOT_PATH = '/';
let staticPrefixConfig = config.getStaticPrefix();
let staticPrefix = path.join(config.getAppPrefix(), staticPrefixConfig);
if (!staticPrefix.startsWith(ROOT_PATH)) {
staticPrefix = ROOT_PATH;
}
if (sysUtils.isWindows()) {
staticPrefix = sysUtils.replaceBackwardSlash(staticPrefix);
}
const staticOptions = {
// one month cache for prod
maxage: DEV_MODE ? 0 : 2592000000,
gzip: false,
setHeaders(res, path) {
if (path.endsWith('.html')) {
res.setHeader('Cache-Control', 'no-cache');
}
},
};
const isStaticAssetsInRoot =
!staticPrefixConfig || staticPrefixConfig === ROOT_PATH;
if (isStaticAssetsInRoot && !servingStaticIndex) {
//workaround: use a random index to pass through the static middleware
staticOptions.index = `${Math.random().toString()}.html`;
}
app.use(
mount(
staticPrefix,
serveStatic(path.join(__dirname, 'build/app'), staticOptions),
),
);
// handle static not found, do not pass further down
if (!isStaticAssetsInRoot) {
app.use(
mount(staticPrefix, (ctx) => {
ctx.status = 404;
ctx.body = 'Not Found';
}),
);
}
// =====serve static end=====
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 hmrPort = config.getHMRPort();
const webpackConfig = require('./config/webpack.config.dev');
const compiler = webpack(
Object.assign({}, webpackConfig, {
stats: {
modules: false,
colors: true,
},
}),
);
const hotClient = {
logLevel: 'error',
hmr: true,
reload: true,
host: {
client: 'localhost',
server: '0.0.0.0',
},
};
if (hmrPort) {
hotClient.port = parseInt(hmrPort);
}
return new Promise((resolve, reject) => {
koaWebpack({
compiler,
hotClient,
devMiddleware: {
index: 'index.html',
publicPath: webpackConfig.output.publicPath,
watchOptions: {
aggregateTimeout: 0,
},
writeToDisk: false,
stats: {
modules: false,
colors: true,
children: false,
},
},
})
.then((middleware) => {
if (!HMRInitialized) {
HMRInitialized = true;
app.use(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))) {
return;
}
if ('string' === typeof API_ENDPOINTS) {
const defaultPrefix = config.getDefaultApiEndPointPrefix();
app.use(handleApiRequests(defaultPrefix, API_ENDPOINTS));
logProxyInfo(API_ENDPOINTS, defaultPrefix);
return;
}
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));
logProxyInfo(endPoint, prefix);
}
}
function logProxyInfo(endPoint, prefix) {
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!`);
},
};