-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
222 lines (177 loc) · 5.55 KB
/
server.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const express = require('express');
const cors = require('cors');
const path = require('path');
const bcrypt = require('bcrypt');
const knex = require('knex');
const compression = require('compression');
const enforce = require('express-sslify');
const helmet = require('helmet');
const xss = require('xss-clean');
const morgan = require('morgan');
const multer = require('multer');
const rateLimit = require('express-rate-limit');
const news = require('./controllers/news');
const weather = require('./controllers/weather');
const search = require('./controllers/search');
const signUp = require('./controllers/sign-up');
const signIn = require('./controllers/sign-in');
const profile = require('./controllers/profile');
const saved = require('./controllers/saved');
const liked = require('./controllers/liked');
const disliked = require('./controllers/disliked');
const hidden = require('./controllers/hidden');
const searches = require('./controllers/searches');
const auth = require('./controllers/authorization');
// App initialization
const app = express();
const port = process.env.PORT || process.env.ONEWS_PORT || 5008;
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config({ path: __dirname + '/.env' });
// Development logging
app.use(morgan('dev'));
}
// Database
const db = knex({
client: 'pg',
connection: {
host: process.env.ONEWS_POSTGRES_HOST,
user: process.env.ONEWS_POSTGRES_USER,
password: process.env.ONEWS_POSTGRES_PASSWORD,
database: process.env.ONEWS_POSTGRES_DB,
},
});
// Multer
const multerStorage = multer.memoryStorage();
const multerFilter = (req, file, cb) => {
if (file.mimetype.startsWith('image')) {
cb(null, true);
} else {
// cb(new AppError('Not an image! Please upload only images!', 400), false);
}
};
const upload = multer({
storage: multerStorage,
fileFilter: multerFilter,
dest: 'uploads/',
});
// Set security HTTP headers
app.use(helmet());
// Data sanitization against XSS
app.use(xss());
app.use(express.json());
// for parsing application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
// for parsing multipart/form-data
app.use(express.static('public'));
// CORS
app.use(cors());
app.options('*', cors());
if (process.env.NODE_ENV === 'production') {
app.use(compression());
app.use(enforce.HTTPS({ trustProtoHeader: true }));
// Serving static files
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'client/build', 'index.html'));
});
}
app.get('/service-worker.ts', (req, res) => {
res.sendFile(path.resolve(__dirname, '..', 'build', 'service-worker.ts'));
});
// Limit requests from same IP
const limiter = rateLimit({
max: 10000,
windowMs: 60 * 60 * 1000,
message: 'Too many requests from this IP, please try again in an hour!',
});
app.use('*', limiter);
// News
app.post('/news', (req, res) => {
news.handleGetNews(req, res);
});
// Weather
app.post('/weather', (req, res) => {
weather.handleGetWeather(req, res);
});
app.post('/search', (req, res) => {
search.handleGetSearch(req, res);
});
app.post('/sign-in', (req, res) => {
signIn.handleSignInAuth(req, res, db, bcrypt);
});
app.post('/sign-up', (req, res) => {
signUp.handleSignUpAuth(req, res, db, bcrypt);
});
// User Profile
app.post('/profile/', auth.requireAuth, (req, res) => {
profile.handleGetProfile(req, res, db);
});
app.patch('/profile', auth.requireAuth, (req, res) => {
profile.handlePatchProfile(req, res, db);
});
app.patch(
'/photo',
auth.requireAuth,
profile.handleUploadPhoto(upload),
(req, res, next) => {
profile.handleResizePhoto(req, res, next);
profile.handlePatchPhoto(req, res, db);
}
);
app.patch('/password', auth.requireAuth, (req, res) => {
profile.handlePatchPassword(req, res, db, bcrypt);
});
// User Saved
app.post('/post-saved', auth.requireAuth, (req, res) => {
saved.handlePostSaved(req, res, db);
});
app.post('/get-saved', auth.requireAuth, (req, res) => {
saved.handleGetSaved(req, res, db);
});
app.post('/delete-saved', auth.requireAuth, (req, res) => {
saved.handleDeleteSaved(req, res, db);
});
// User Liked
app.post('/post-liked', auth.requireAuth, (req, res) => {
liked.handlePostLiked(req, res, db);
});
app.post('/get-liked', auth.requireAuth, (req, res) => {
liked.handleGetLiked(req, res, db);
});
app.post('/delete-liked', auth.requireAuth, (req, res) => {
liked.handleDeleteLiked(req, res, db);
});
// User Disliked
app.post('/post-disliked', auth.requireAuth, (req, res) => {
disliked.handlePostDisliked(req, res, db);
});
app.post('/get-disliked', auth.requireAuth, (req, res) => {
disliked.handleGetDisliked(req, res, db);
});
app.post('/delete-disliked', auth.requireAuth, (req, res) => {
disliked.handleDeleteDisliked(req, res, db);
});
// User Hidden
app.post('/post-hidden', auth.requireAuth, (req, res) => {
hidden.handlePostHidden(req, res, db);
});
app.post('/get-hidden', auth.requireAuth, (req, res) => {
hidden.handleGetHidden(req, res, db);
});
app.post('/delete-hidden', auth.requireAuth, (req, res) => {
hidden.handleDeleteHidden(req, res, db);
});
// User Searches
app.post('/post-searches', auth.requireAuth, (req, res) => {
searches.handlePostSearches(req, res, db);
});
app.post('/get-searches', auth.requireAuth, (req, res) => {
searches.handleGetSearches(req, res, db);
});
app.post('/delete-searches', auth.requireAuth, (req, res) => {
searches.handleDeleteSearches(req, res, db);
});
app.listen(port, (error) => {
if (error) throw error;
console.log('Server running on port ' + port);
});