-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
50 lines (41 loc) · 1.37 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
const express = require('express');
const logger = require('morgan');
const cors = require('cors');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
require('dotenv').config();
const categoriesRouter = require('./routes/api/categories');
const userRouter = require('./routes/api/user');
const transactionsRouter = require('./routes/api/transactions');
const reportsRouter = require('./routes/api/reports');
const app = express();
const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short';
app.use(logger(formatsLogger));
// app.use(cors());
app.use(
cors({
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
// preflightContinue: false,
// optionsSuccessStatus: 204,
}),
);
app.use(express.json());
app.use(express.static('public'));
// подключаем swagger
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use('/api/categories', categoriesRouter);
app.use('/api/user', userRouter);
app.use('/api/transactions', transactionsRouter);
app.use('/api/reports', reportsRouter);
app.use((req, res) => {
res.status(404).json({
status: 'error',
message: 'Not found',
});
});
app.use((err, req, res, next) => {
const { status = 500, message = err.message } = err;
res.status(status).json({ status: 'error', code: status, message });
});
module.exports = app;