-
Notifications
You must be signed in to change notification settings - Fork 20
/
server.js
59 lines (48 loc) · 1.36 KB
/
server.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
const url = require('url');
const path = require('path');
const express = require('express');
// 代理转发
const proxy = require('http-proxy-middleware');
// 日志库
const expressWinston = require('express-winston');
const winston = require('winston');
const helmet = require('helmet');
const compression = require('compression');
// 环境变量
const env = process.env.NODE_ENV || 'development';
const isProd = env === 'production' ? true : false;
// App配置
const config = isProd ? require('./config.prod.json')
: require('./config.dev.json');
// if (config.watcher) {
// require(config.watcher);
// }
const app = express();
// 设置安全的 HTTP Header
app.use(helmet());
// 开启 gzip 压缩
app.use(compression());
// 静态资源
app.use(express.static(path.join(__dirname, 'dist')));
// 代理转发
app.use('/replace', proxy({
target: config.api, // target host
changeOrigin: true, // needed for virtual hosted sites
pathRewrite: {
'^/replace' : '' // remove base path
}
}));
// express-winston errorLogger makes sense AFTER the router.
app.use(
expressWinston.logger({
transports: [
new winston.transports.Console({
json: true,
colorize: true
})
]
})
);
app.listen(config.port, () => {
console.log(`🌎 => Server is running on port ${config.port}`);
});