-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (42 loc) · 1.42 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
const express = require('express');
const mongoose = require('mongoose');
require('dotenv').config();
const { PORT = 3000 } = process.env;
const bodyParser = require('body-parser');
const { errors } = require('celebrate');
const auth = require('./middlewares/auth');
const { authValidator } = require('./middlewares/validators/authValidator');
const errorHandler = require('./middlewares/errorHandler');
const { logger, errorLogger } = require('./middlewares/logger');
const { login } = require('./controllers/login');
const { createUser } = require('./controllers/createUser');
const cardsRoute = require('./routes/cards');
const usersRoute = require('./routes/users');
const app = express();
mongoose.connect('mongodb://localhost:27017/mestodb', {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
});
app.use(
bodyParser.urlencoded({
extended: true,
}),
);
app.use(bodyParser.json());
app.use(logger);
app.post('/signin', authValidator, login);
app.post('/signup', authValidator, createUser);
app.use(auth);
app.use('/', express.static('./public'));
app.use('/users', usersRoute);
app.use('/cards', cardsRoute);
app.use('*', (req, res) => {
res.status(404).send({ message: 'Запрашиваемый ресурс не найден' });
});
app.use(errorLogger);
app.use(errors());
app.use(errorHandler);
app.listen(PORT, () => {
console.log(`Application is running on port ${PORT}`);
});