app.js
1.37 KB
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
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const Express = require('express');
const pkg = require('../package.json');
const SqlHelper = require('./utils/mysql');
const favicon = require('serve-favicon');
const path = require('path');
global.env = {
version: pkg.version,
Production: process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'gray',
Gray: process.env.NODE_ENV === 'gray',
Test: (process.env.NODE_ENV || '').indexOf('test') >= 0
};
let app = new Express();
const config = require('./common/config');
global.yoho = {
config
};
global.yoho.utils = {
mysqlCli: new SqlHelper(config.mysql.database)
};
app.use(favicon(path.join(__dirname, '/favicon.ico')));
app.use(Express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json({
limit: 1 * 1024 * 1024 // 限制请求长度1m
}));
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.get(/\.html$/, (req, res) => {
res.sendfile(path.join(__dirname, 'public/index.html'));
});
try {
const error = require('./core/error');
require('./dispatch')(app);
// 错误处理
app.all('*', error.pageNotFound);
app.use(error.serverError);
} catch (err) {
console.error(err);
}
app.listen(config.port, () => {
console.info(`${pkg.name} start at http://localhost:${config.port}/`);
});