-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
113 lines (90 loc) · 2.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
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
const express = require('express');
const mongoose = require('mongoose');
const { celebrate, Joi, errors } = require('celebrate');
const cors = require('cors');
const rateLimit = require('express-rate-limit');
const slowDown = require('express-slow-down');
const helmet = require('helmet');
const dotenv = require('dotenv');
dotenv.config();
const { requestLogger, errorLogger } = require('./middleware/logger');
const auth = require('./middleware/auth');
const userRouter = require('./routers/users');
const cardRouter = require('./routers/cards');
const { createUser, login } = require('./controllers/users');
const NotFoundError = require('./errors/NotFoundError');
const { serverErrorHandler } = require('./utils/serverErrorHandler');
const { PORT = 3001, NODE_ENV, MONGO_SECRET } = process.env;
const app = express();
const mongodbUri =
NODE_ENV === 'production'
? `mongodb+srv://derekschinke:${MONGO_SECRET}@practicum.0zhvm.mongodb.net/arounddb?retryWrites=true&w=majority`
: 'mongodb://localhost:27017/arounddb';
mongoose.connect(mongodbUri, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
});
app.use(cors());
app.options('*', cors());
app.set('trust proxy', 1);
const limiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
});
app.use(limiter);
app.enable('trust proxy');
const speedLimiter = slowDown({
windowMs: 15 * 60 * 1000,
delayAfter: 100,
delayMs: 500,
});
app.use(speedLimiter);
app.use(helmet());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(requestLogger);
app.use('/users', auth, userRouter);
app.use('/cards', auth, cardRouter);
app.get('/crash-test', () => {
setTimeout(() => {
throw new Error('Server will crash now');
}, 0);
});
app.post(
'/signup',
celebrate({
body: Joi.object().keys({
name: Joi.string().min(2).max(30),
about: Joi.string().min(2).max(30),
avatar: Joi.string().uri({ scheme: ['http', 'https'] }),
email: Joi.string().email().required(),
password: Joi.string().min(8).required(),
}),
}),
createUser
);
app.post(
'/signin',
celebrate({
body: Joi.object().keys({
email: Joi.string().email().required(),
password: Joi.string().min(8).required(),
}),
}),
login
);
app.use(errorLogger);
app.use(errors());
app.use('*', (req, res, next) =>
next(new NotFoundError('Requested resource not found'))
);
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
serverErrorHandler(err, res);
});
app.listen(process.env.PORT || PORT, () => {
// eslint-disable-next-line no-console
console.log(`App listening at port ${PORT}`);
});