-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
52 lines (35 loc) · 1.02 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
const express = require('express');
const mongoose = require('mongoose');
const helmet = require('helmet');
const cors = require('cors');
const { errors } = require('celebrate');
const { requestLogger, errorLogger } = require('./middleware/logger');
const { PORT, DATABASE } = require('./config/constants');
const { limiter } = require('./middleware/limiter');
const routes = require('./routes/index');
const errorHandler = require('./errors/errorHandler');
const app = express();
app.use(cors());
app.options('*', cors());
app.use(limiter);
mongoose.connect(DATABASE, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
});
app.use(helmet());
app.use(express.json());
app.use(requestLogger);
// for crash testing, remove after review
app.get('/crash-test', () => {
setTimeout(() => {
throw new Error('Server will crash now');
}, 0);
});
app.use('/', routes);
app.use(errorLogger);
// celebrate error handler
app.use(errors());
app.use(errorHandler);
app.listen(PORT);