-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
164 lines (108 loc) · 4.71 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
152
153
154
155
156
157
158
159
160
161
162
163
164
'use strict';
const express = require('express');
const app = express();
const expressValidator = require('express-validator');
// В коде удобней различать среды по ИМЕНИ.
const developmentFlag = app.get('env') !== 'production';
const productionFlag = app.get('env') === 'production';
const compression = require('compression');
const HttpStatus = require('http-status-codes');
app.use(compression());
const passport = require('passport');
const path = require('path');
const favicon = require('serve-favicon');
const httpLogger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const session = require('express-session');
const csurf = require('csurf');
// Наши модули
const config = require('config');
const mongoose = require('./utils/mongoose');
const logger = require('./utils/log')(module);
require('./utils/passport')();
const localStrategy = require('./utils/passport/local');
const vkStrategy = require('./utils/passport/vk');
app.use(require('./middlewares/send-http-error'));
// view engine setup (Т.к. у нас уже написан html, лучше пока не юзать движки)
//app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'jade');
// Статику подключаем только в development среде!
// В production за выдачу статики отвечает nginx
if (developmentFlag) {
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(express.static(path.join(__dirname, 'public')));
}
app.use(httpLogger('dev'));
app.use(bodyParser.json()); // Парсер json в потоках
app.use(bodyParser.urlencoded({extended: false}));
app.use(expressValidator());
app.use(cookieParser());
// Сторедж для сессии.
const MongoStore = require('connect-mongo')(session);
app.use(session({
secret: config.get('session:secret'), // ABCDE242342342314123421.SHA256
key: config.get('session:key'),
resave: config.get('session:resave'),
saveUninitialized: config.get('session:saveUninitialized'),
cookie: config.get('session:cookie'),
rolling: config.get('session:rolling'),
store: new MongoStore({
mongooseConnection: mongoose.connection,
stringify: false
})
}));
// init passportJS
app.use(passport.initialize());
app.use(passport.session());
// CSRF проверку делаем ТОЛЬКО в production.
// В development не имеет смысла :)
if (productionFlag) {
app.use(csurf());
app.use(function (req, res, next) {
res.cookie('XSRF-TOKEN', req.csrfToken());
return next();
});
}
passport.use('local-login', localStrategy.login);
passport.use('vk-login', vkStrategy.login);
// инициализируем api;
require('./routes')(app);
// Выдаем стартовую страницу ангуляра,на случай неразрешения роута (для html5 mode).
app.use('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
const HttpError = require('error').HttpError;
app.use(function (err, req, res, next) {
// Если ошибка при валидации CSRF
if (err.code === 'EBADCSRFTOKEN') {
res.status(HttpStatus.FORBIDDEN);
return res.send('You cannot do that from outside of our client side ;)')
}
// На случай, если из какого-либо middleware выпала ошибка, отличная от числа или HttpError.
// Это говорит о том, что в err скрыт объект, который создала какая-либо сторонняя библиотека (к примеру, mongoose).
// Нет необходимости раскрывать ее описание пользователю. Достаточно будет скрыть ее за 500 кодом.
if (!err instanceof HttpError && !typeof err == 'number') {
err = HttpStatus.INTERNAL_SERVER_ERROR;
}
// Проверка на число. У нас устоялся подход, что для удобства, в некоторых местах кода,
// в качестве ошибки мы просто указываем Http код.
if (typeof err == 'number') {
err = new HttpError(err);
}
if (developmentFlag) {
logger.error(err);
}
// middlewares/sendHttpError
res.sendHttpError(err);
});
if (developmentFlag) {
var maxHeap = 0;
setInterval(function () {
var heap = process.memoryUsage().heapUsed;
maxHeap = maxHeap < heap ? heap : maxHeap;
logger.info('Heap size: ' + heap + ', maximum heap size: ' + maxHeap);
},
10000);
}
module.exports = app;