-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
65 lines (58 loc) · 2.39 KB
/
Copy pathapp.js
File metadata and controls
65 lines (58 loc) · 2.39 KB
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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const { errors } = require('celebrate');
const fs = require('fs');
const { login, createUser, logout } = require('./controllers/user');
const auth = require('./middlewares/auth');
const ErrorHandler = require('./errors/ErrorHandler');
const { validateNewUser, validateCredentials } = require('./middlewares/celebrations');
const { requestLogger, errorLogger } = require('./middlewares/logger');
const NotFoundError = require('./errors/NotFoundError');
const app = express();
const { PORT = 3000 } = process.env;
mongoose.connect('mongodb://localhost:27017/mestodb');
// для разработки
/* eslint-disable no-console */
fs.unlink('./error.log', console.log);
fs.unlink('./request.log', console.log);
/* eslint-enable no-console */
app.use(cors({
origin: ['https://multipasport.nomoredomains.icu', 'http://localhost:3000', 'http://localho.st:3000', 'http://fdmitrij.nomoredomains.icu', 'https://fdmitrij.nomoredomains.icu'],
credentials: true,
}));
app.use(bodyParser.json()); // для собирания JSON-формата
app.use(cookieParser()); // парсер кук
app.use(requestLogger); // подключаем логгер запросов
app.get('/crash-test', () => {
setTimeout(() => {
throw new Error('Сервер сейчас упадёт');
}, 0);
});
app.post('/signin', validateCredentials, login);
app.post('/signup', validateNewUser, createUser);
// авторизация
app.use(auth);
// роуты
app.post('/logout', logout);
app.use('/users', require('./routes/users'));
app.use('/cards', require('./routes/cards'));
// ошибки
app.use('/', (req, res, next) => {
next(new NotFoundError('Ресурс не найден. Проверьте URL и метод запроса'));
});
app.use(errorLogger); // подключаем логгер ошибок
app.use(errors()); // обработчик ошибок celebrate
app.use(ErrorHandler);
app.listen(
PORT,
() => {
// Если всё работает, консоль покажет, какой порт приложение слушает
// запрещено линтером, поэтому
/* eslint-disable no-console */
console.log(`App listening on port ${PORT}`);
/* eslint-enable no-console */
},
);