-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (84 loc) · 3.31 KB
/
index.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
const glob = require('glob');
const express = require('express');
const mongoose = require('mongoose');
const helmet = require('helmet');
const bodyParser = require('body-parser');
const morgan = require('morgan');
// ----------------------------------------------------- load express app
const app = express();
app.all('*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By", ' 3.2.1')
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
app.use(helmet());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(morgan('dev'))
// ----------------------------------------------------- load config
if (process.env.NODE_ENV === 'dev'){
app.config = require('./app/config/config_dev');
} else if (process.env.NODE_ENV === 'prod'){
app.config = require('./app/config/config_prod');
}
// ----------------------------------------------------- load logger
const Logger = require('egg-logger').Logger;
const FileTransport = require('egg-logger').FileTransport;
const ConsoleTransport = require('egg-logger').ConsoleTransport;
const logger = new Logger();
if(app.config.log.file){
logger.set('file', new FileTransport({
file: app.config.log.file.path,
level: app.config.log.file.level,
formatter: loggerFormatter
}));
}
if(app.config.log.console){
logger.set('console', new ConsoleTransport({
level: app.config.log.console.level,
formatter: loggerFormatter
}));
}
app.logger = logger;
// ----------------------------------------------------- global init
var init_list = glob.sync(app.config.root + 'app/init/*.js');
init_list.forEach(function (f) {
app.logger.info('attached init:' + f)
var init_func = require(f)
if (typeof init_func === 'function'){
init_func(app);
}
});
// ----------------------------------------------------- load middleware (first)
var middle_list = glob.sync(app.config.root + 'app/middleware/*.js');
middle_list.forEach(function (f) {
app.logger.info('attached middleware:' + f)
var middle_func = require(f)
if (typeof middle_func === 'function'){
app.use(middle_func);
}
});
// ----------------------------------------------------- global last
var last_list = glob.sync(app.config.root + 'app/last/*.js');
last_list.forEach(function(f) {
app.logger.info('attached last:' + f)
var last_func = require(f)
if (typeof last_func === 'function') {
last_func(app);
}
});
// ----------------------------------------------------- load router (second)
require(app.config.root + "app/router")(app);
// ----------------------------------------------------- load error handler (last)
// 加载路由完成后才能加载错误处理中间件
app.use(require(app.config.root + 'app/error_handler.js'));
// ----------------------------------------------------- start server
app.listen(app.config.server.port, function () {
logger.info("server started on port:" + app.config.server.port);
});
function loggerFormatter(meta) {
return meta.date + ' ' + meta.level + ' ' + meta.pid + ' ' + meta.message;
}