-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.js
executable file
·85 lines (65 loc) · 1.93 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 morgan = require('morgan');
const bodyParser = require('body-parser');
const cors = require('cors');
const helmet = require('helmet');
const path = require('path');
const logger = require('./config/logger');
const { devProfile } = require('./config/morganConfig');
const { directives, limiter, options } = require('./config/middlewares');
const {
userRouter,
forumRouter,
tagRouter,
chatRouter,
notificationsRouter,
universityRouter,
} = require('./routes');
// --- App config
const app = express();
// --- Middleware
app.set('json spaces', 2);
// body-parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(bodyParser.raw());
// morgan
app.use(morgan(devProfile));
// express-rate-limit
app.use(limiter);
// helmet
app.use(helmet());
app.use(helmet.contentSecurityPolicy({ directives }));
// app.use(helmet.noCache()); //helmet noCache is deprecated
// cors
app.use(cors(options));
// --- Routes
app.use('/api/users', userRouter);
app.use('/api/tags', tagRouter);
app.use('/api/chats', chatRouter);
app.use('/api/forum', forumRouter);
app.use('/api/notifications', notificationsRouter);
app.use('/api/university', universityRouter);
// --- Documentation
app.use('/docs/models', express.static(path.join(__dirname, '/docs/models')));
app.use('/docs/routes', express.static(path.join(__dirname, '/docs/routes')));
app.use(express.static(path.join(__dirname, 'webapp', 'build')));
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'webapp', 'build', 'index.html'));
});
// TODO: add 404 resource not found route
/**
* Error handler.
* Sends 400 for Mongoose validation errors.
* 500 otherwise.
* Do all error handling here.
*/
app.use((err, req, res, next) => {
logger.error(err);
if (err.name === 'ValidationError') {
return res.status(400).json(err.errors);
}
return res.status(500).json(err);
});
// ---
module.exports = app;