-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
85 lines (66 loc) · 2.54 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
const express = require('express');
const path = require('path');
const helmet = require('helmet');
const cors = require('cors');
const rateLimit = require("express-rate-limit");
const compression = require('compression');
const morgan = require('morgan')
const favicon = require('serve-favicon');
// const HttpError = require('./models/http-error');
// controller imports
const authController = require('./controllers/authController');
const postController = require('./controllers/postController');
const userController = require('./controllers/userController');
const categoryController = require('./controllers/categoryController');
const adminController = require('./controllers/adminController');
const privateGroupController = require('./controllers/privateGroupController');
const interviewCornerController = require('./controllers/interviewCornerController');
const interviewExperienceController = require('./controllers/interviewExperienceController');
const videoStreamingController = require('./controllers/videoStreamingController');
const { deleteFile, upload } = require('./models/db');
// Init
const app = express();
app.use(favicon(path.join(__dirname, 'client', 'public', 'favicon.ico')))
app.use(helmet({
contentSecurityPolicy: false,
}));
app.use(cors());
app.use(compression())
app.set('trust proxy', 1);
const apiLimiter = rateLimit({
windowMs: 60 * 60 * 1000, // 1hr
max: 100,
message: 'Too many requests from this IP,please try again later.'
});
// limit body data at 300kb only
app.use(express.json({ limit: '300kb' }));
app.use(express.urlencoded({extended:false}));
app.use(morgan('tiny'))
// Registered Middlewares
app.use('/register', authController);
app.use('/category', categoryController);
app.use('/posts', postController);
app.use('/user', userController);
app.use('/admin', adminController);
app.use('/privategroup', privateGroupController);
app.use('/streamer', videoStreamingController);
app.use('/interviewCorner', interviewCornerController);
app.use('/experience', interviewExperienceController);
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'))
});
}
// Error Handling Middleware
app.use((error, req, res, next) => {
if (req.file) {
deleteFile(next, req.file.id)
}
if (res.headerSent) {
return next(error)
}
res.status(error.code || 500)
res.json({ message: error.message || 'An Unknown Error has Occured' })
})
module.exports = app;