-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
63 lines (50 loc) · 1.65 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
const express = require('express');
const cors = require('cors');
const helmet = require('helmet');
const cookieParser = require('cookie-parser');
const mongoSanitize = require('express-mongo-sanitize');
const xss = require('xss-clean');
const path = require('path');
const middleware = require('./utils/middleware');
const clientEndpoints = ['discover', 'profile', 'update'];
const searchRouter = require('./routes/searchRoutes');
const userRouter = require('./routes/userRoutes');
const authRouter = require('./routes/authRoutes');
const adminRouter = require('./routes/adminRoutes');
const AppError = require('./utils/appError');
const globalErrorHandler = require('./controller/errorController');
require('./cronJobs/backup');
const app = express();
app.use(helmet());
app.use(
cors({
origin: 'http://localhost:3001',
credentials: true,
})
);
app.use(xss());
app.use(cookieParser());
app.use(express.json({ limit: '10kb' }));
app.use(mongoSanitize());
app.use(middleware.requestLogger);
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('/:clientEndpoint', (req, res, next) => {
if (clientEndpoints.includes(req.params.clientEndpoint)) {
res.sendFile(path.join(__dirname, '/client/build/index.html'));
} else {
next();
}
});
//Search Endpoints
app.use('/api/v1/search', searchRouter);
//User Endpoints
app.use('/api/v1/user', userRouter);
//Authentication Endpoint
app.use('/api/v1/auth', authRouter);
//Admin Endpoints
app.use('/api/v1/admin', adminRouter);
app.all('*', (req, res, next) => {
next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404));
});
app.use(globalErrorHandler);
module.exports = app;