-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (50 loc) · 1.93 KB
/
index.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
const express = require('express')
require('dotenv').config()
const cors = require('cors')
const fileUpload = require('express-fileupload')
const path = require('path')
const sequelize = require('./db')
const Models = require('./models/models')
const AuthorizationMiddleware = require('./middleware/authorization')
const {graphqlHTTP} = require('express-graphql')
const schema = require('./api/qraphql/schema')
const root = require('./controllers/qraphQL/index')
const routers = require("./api/routers")
const defaultData = require("./workers/defolt-data")
const deleteOldFiles = require('./workers/remove-photos')
const checkAuth = require('./middleware/authorization')
/** CONSTANTS---------------------------------------*/
const PORT = process.env.PORT;
const app = express();
app.use(express.json());
app.use(express.static(path.resolve(__dirname, 'static')));
app.use(cors());
app.use(fileUpload());
app.use(AuthorizationMiddleware);
app.use('/graphql', checkAuth, graphqlHTTP({
graphiql: true,
schema,
rootValue: root,
// formatError: (err) => {
// return err.message
// }
}))
app.use('/api', checkAuth, routers)
/**CONSTANTS---------------------------------------*/
/**START APP----------------------------------------*/
const start = async () => {
try {
await sequelize.authenticate(); /** подключение к БД*/
await sequelize.sync({alter: true}); /** сверяем БД с теми моделями что мы описали*/
await defaultData(); /** создаем дефолтные роли*/
await app.listen(PORT, () => console.log(`SERVER START ON - http://localhost:${PORT}`))
await deleteOldFiles()
} catch (e) {
console.log(`ERROR - ${e}`) /** отлавливаем ошибку при подключении*/
}
}
start();
/** START APP----------------------------------------*/
app.get('/', (req, res) => {
res.json('SERVER START');
})