-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
151 lines (127 loc) · 3.45 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
/*!
* nodeclub - app.js
*/
/**
* Module dependencies.
*/
var config = require('./config');
/*if (!config.debug) {
require('newrelic');
}
*/
var path = require('path');
var Loader = require('loader');
var express = require('express');
var session = require('express-session');
var passport = require('passport');
require('./models');
var GitHubStrategy = require('passport-github').Strategy;
var githubStrategyMiddleware = require('./middlewares/github_strategy');
var webRouter = require('./web_router');
var apiRouterV1 = require('./api_router_v1');
var auth = require('./middlewares/auth');
var MongoStore = require('connect-mongo')(session);
var _ = require('lodash');
var csurf = require('csurf');
var compress = require('compression');
var bodyParser = require('body-parser');
var busboy = require('connect-busboy');
var errorhandler = require('errorhandler');
var cors = require('cors');
// 静态文件目录
var staticDir = path.join(__dirname, 'public');
// assets
var assets = {};
if (config.mini_assets) {
try {
assets = require('./assets.json');
} catch (e) {
console.log('You must execute `make build` before start app when mini_assets is true.');
throw e;
}
}
var urlinfo = require('url').parse(config.host);
config.hostname = urlinfo.hostname || config.host;
var app = express();
// configuration in all env
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
app.engine('html', require('ejs-mate'));
app.locals._layoutFile = 'layout.html';
app.use(require('response-time')());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(require('method-override')());
app.use(require('cookie-parser')(config.session_secret));
app.use(compress());
app.use(session({
secret: config.session_secret,
store: new MongoStore({
url: config.db
}),
resave: true,
saveUninitialized: true,
}));
app.use(passport.initialize());
// custom middleware
app.use(auth.authUser);
app.use(auth.blockUser());
app.use(Loader.less(__dirname));
app.use('/public', express.static(staticDir));
if (!config.debug) {
app.use(function (req, res, next) {
if (req.path.indexOf('/api') === -1) {
csurf()(req, res, next);
return;
}
next();
});
app.set('view cache', true);
}
// for debug
// app.get('/err', function (req, res, next) {
// next(new Error('haha'))
// });
// set static, dynamic helpers
_.extend(app.locals, {
config: config,
Loader: Loader,
assets: assets
});
_.extend(app.locals, require('./common/render_helper'));
app.use(function (req, res, next) {
res.locals.csrf = req.csrfToken ? req.csrfToken() : '';
next();
});
// github oauth
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
passport.use(new GitHubStrategy(config.GITHUB_OAUTH, githubStrategyMiddleware));
app.use(busboy({
limits: {
fileSize: 10 * 1024 * 1024 // 10MB
}
}));
// routes
app.use('/api/v1', cors(), apiRouterV1);
app.use('/', webRouter);
// error handler
if (config.debug) {
app.use(errorhandler());
} else {
app.use(function (err, req, res, next) {
return res.status(500).send('500 status');
});
}
app.listen(config.port, function () {
console.log("NodeClub listening on port %d in %s mode", config.port, app.settings.env);
console.log("God bless love....");
console.log("You can debug your app with http://" + config.hostname + ':' + config.port);
});
module.exports = app;