-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
184 lines (160 loc) · 8.37 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const _ = require('lodash');
const aws = require('aws-sdk');
const articleRoute = require('./modern-backbone-starterkit/src/isomorphic/routes').articleRoute;
const bodyParser = require('body-parser');
const categories = require('./modern-backbone-starterkit/src/isomorphic/categories').categories;
const escapeUserInfo = require('./modern-backbone-starterkit/src/isomorphic/utils').escapeUserInfo;
const express = require('express');
const logError = require('./server_code/utils').logError;
const MongoClient = require('mongodb').MongoClient;
const multer = require('multer');
const passport = require('passport');
const RateLimit = require('express-rate-limit');
const send404 = require('./server_code/utils').send404;
const session = require('express-session');
const setupAuthentication = require('./server_code/authentication');
const setupInitialConfiguration = require('./server_code/configuration');
const timebucket = require('timebucket');
const updateSummaries = require('./server_code/updateSummaries');
const userInfo = require('./server_code/routeFunctions/getUserInfoJSON');
const url = require('url');
const wtimeout = require('./server_code/utils').wtimeout;
const app = express();
const upload = multer();
const distDir = __dirname + '/modern-backbone-starterkit/dist/';
const MONGO_URI = process.env.MONGODB_URI;
MongoClient.connect(MONGO_URI,
{
db: {
j: true,
//readConcern:'majority',
wtimeout: wtimeout,
w:'majority',
}
},
(err, db) => {
if (err !== null) {
logError(err);
throw err;
} else {
setupInitialConfiguration(app);
setupAuthentication(app, db);
const sendIndex = function(req, res) {
let currentUserPromise;
if (req.user) {
currentUserPromise = userInfo.getUserInfoJSON(db, req.user.fbId, true);
} else {
currentUserPromise = Promise.resolve(userInfo.notLoggedInUserInfoJSON());
}
currentUserPromise.then(function(currentUser) {
escapeUserInfo(currentUser);
res.render('pages/index', {
currentUser: JSON.stringify(currentUser),
fbAppId: process.env.FACEBOOK_APP_ID,
imageBaseUrl: process.env.IMAGE_BASE_URL
});
}).catch(function(err) {
next(err);
});
}
const refreshFacebookInfoOrLogout = require('./server_code/updateUser')(db);
app.get('/terms-and-conditions', function(req, res) {
res.render('pages/termsAndConditions', {});
});
app.get('/privacy-policy', function(req, res) {
res.render('pages/privacyPolicy', {});
});
app.get('/dmca-instructions', function(req, res) {
res.render('pages/dmcaInstructions', {});
});
app.get('/after-upload-auth', refreshFacebookInfoOrLogout, function(req, res) {
res.render('pages/afterUploadAuth', {});
});
// IMPORTANT: Routes are duplicated in client side code.
// Namely the router and the nav template.
app.get('/', sendIndex);
app.get('/about', sendIndex);
app.get('/admin/flagged-articles', sendIndex);
app.get('/admin/my-approval-history', sendIndex);
app.get('/admin/need-approval-articles', sendIndex);
app.get('/admin/need-approval-images', sendIndex);
app.get('/admin/upload', sendIndex);
app.get('/flags/:articleId', sendIndex);
app.get('/user/:userid', sendIndex);
app.get('/upload', refreshFacebookInfoOrLogout, sendIndex);
for (let i=0; i < categories.length; i++) {
app.get('/' + categories[i].urlSlug, sendIndex);
}
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
//const approveArticles = require('./server_code/routeFunctions/approveArticles')(db);
const approveImages = require('./server_code/routeFunctions/approveImages')(db);
const bestArticlesJSON = require('./server_code/routeFunctions/bestArticlesJSON')(db);
const getArticleJSON = require('./server_code/routeFunctions/getArticleJSON')(db);
const getArticlePage = require('./server_code/routeFunctions/getArticlePage')(db);
const getFeaturedImagesJSON = require('./server_code/routeFunctions/getFeaturedImagesJSON')(db);
const getMostRecentArticlesJSON = require('./server_code/routeFunctions/getMostRecentArticlesJSON')(db);
const getMyApprovalHistoryJSON = require('./server_code/routeFunctions/getMyApprovalHistoryJSON')(db);
const getMyAuthoredArticles = require('./server_code/routeFunctions/getMyAuthoredArticles')(db);
const getNeedApprovalArticlesJSON = require('./server_code/routeFunctions/getNeedApprovalArticlesJSON')(db);
const getNeedApprovalImagesJSON = require('./server_code/routeFunctions/getNeedApprovalImagesJSON')(db);
const getTextSearchImages = require('./server_code/routeFunctions/getTextSearchImages')(db);
const getUserInfo = require('./server_code/routeFunctions/getUserInfoJSON').getRouteFunction(db);
const mostViewedArticlesJSON = require('./server_code/routeFunctions/mostViewedArticlesJSON')(db);
const postApprovalNotification = require('./server_code/routeFunctions/postApprovalNotification')(db);
const postArticle = require('./server_code/routeFunctions/postArticle')(db);
const getArticleFlags = require('./server_code/routeFunctions/getArticleFlags')(db);
const patchArticle = require('./server_code/routeFunctions/patchArticle')(db);
const postFlagArticle = require('./server_code/routeFunctions/postFlagArticle')(db);
const postFlaggedArticles = require('./server_code/routeFunctions/postFlaggedArticles')(db);
const postImage = require('./server_code/routeFunctions/postImage')(db);
const postListArticles = require('./server_code/routeFunctions/postListArticles')(db);
const postVote = require('./server_code/routeFunctions/postVote')(db);
//app.post('/approve-articles', bodyParser.urlencoded({extended: true}), approveArticles);
app.post('/approve-images', bodyParser.urlencoded({extended: true}), approveImages);
app.post('/best-articles', bodyParser.json(), bestArticlesJSON);
app.get('/article-flags', bodyParser.json(), getArticleFlags);
app.get('/api/article', getArticleJSON);
app.get(articleRoute.nodeRouteString, getArticlePage);
app.get('/featured-images', getFeaturedImagesJSON);
app.get('/most-recent-articles', getMostRecentArticlesJSON);
app.get('/api/my-approval-history', getMyApprovalHistoryJSON);
app.get('/my-authored-articles', getMyAuthoredArticles);
app.get('/text-search-images', getTextSearchImages);
app.get('/articles-that-need-approval', getNeedApprovalArticlesJSON);
app.get('/images-that-need-approval', getNeedApprovalImagesJSON);
app.get('/userinfo', getUserInfo);
// most-viewed-articles uses post instead of get to get over query string length limitations
app.post('/most-viewed-articles', bodyParser.json(), mostViewedArticlesJSON);
app.patch('/article', bodyParser.urlencoded({extended: false}), patchArticle);
app.post('/article-approval-notification', bodyParser.urlencoded({extended: false}), postApprovalNotification);
app.post('/article', bodyParser.urlencoded({extended: false}), postArticle);
app.post('/flag-article', bodyParser.urlencoded({extended: true}), postFlagArticle);
app.post('/flagged-articles', bodyParser.json(), postFlaggedArticles);
const imageLimiter = new RateLimit({
windowMs: 1000*60, // 1 minute
max: 5, // limit each IP to 5 requests per windowMs
});
app.post('/image', imageLimiter, upload.single('image'), postImage);
app.post('/list-articles', bodyParser.urlencoded({extended: true}), postListArticles);
const voteLimiter = new RateLimit({
delayAfter: 1, // begin slowing down responses after the first request
delayMs: 5000, // slow down subsequent responses by 5 second per request
});
app.post('/vote', voteLimiter, bodyParser.urlencoded({extended: false}), postVote);
app.use(express.static(distDir));
app.use(express.static('public'));
app.use(function(req, res, next) {
send404(res);
});
// views is directory for all template files
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
}
}
);