-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
91 lines (83 loc) · 2.58 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
let express = require('express');
let path = require('path');
let favicon = require('serve-favicon');
let logger = require('morgan');
let cookieParser = require('cookie-parser');
let bodyParser = require('body-parser');
// let res_api = require('res.api');
var os = require('os');
var http = require('http');
var cors = require('cors');
var compression = require('compression');
// 引入 mongoose 配置文件,执行配置文件中的函数,以实现数据库的配置和 Model 的创建等
let mongoose = require('./app/config/mongoose.js');
let db = mongoose();
// 主页及后台管理页面
let web = require('./app/routes/web.routes.js');
// api入口
let api = require('./app/routes/api.routes.js');
// oauth 相关接口
let oauth = require('./app/routes/oauth.routes.js');
let app = express();
app.use(cors());
// view engine setup
app.set('views', path.join(__dirname, 'app/views'));
app.set('view engine', 'hbs');
// app.set('env', 'development');
app.set('env', 'production');
// uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
// app.use(res_api);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
// app.use(require('node-compass')({mode: 'expanded'}));
app.use(compression()); // use compression
app.use(express.static(path.join(__dirname, 'public'), {
etag: false, // 资源标记
maxAge: 0, // 30 days 后过期, 单位ms
setHeaders: function (res, path, state) {
if (/\.(js|css|png|gif|jpg|jpeg|ico|mp3)$/.test(path)) {
// 未来的一个过期时间
res.set('Expires', new Date(Date.now() + 2592000 * 1000).toGMTString())
res.set('Cache-Control', 'public, max-age=2592000')
}
}
}));
app.use('/', web);
app.use('/api', api);
app.use('/oauth', oauth);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
let err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.set('Content-Type', 'text/html');
res.render('error_dev', {
title: err.status,
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
console.log(err)
res.status(err.status || 500);
res.set('Content-Type', 'text/html');
res.render('error', {
title: '我们一定能找到你'
});
});
module.exports = app;