-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
100 lines (81 loc) · 2.58 KB
/
server.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
/* const bodyParser = require('body-parser')
const express = require('express')
const app = express()
const path = require('path')
const User = require ('./app/models/user.model')
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json())
const dbConfig = require('./config/database.config')
const mongoose = require('mongoose')
const { error } = require('console')
mongoose.Promise = global.Promise
/mongoose.connect(dbConfig.url, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() =>{
console.log('Успешное подключение к базе данных')
}).catch((error) => {
console.log('Нет соединения с базой данных', error)
process.exit()
})
app.use(express.static(path.join(__dirname, 'public')))
app.get('/', (request, response) => {
response.sendFile(`${__dirname}/public/html/auth.html`)
})
app.get('/main/', (request, response) => {
response.sendFile(`${__dirname}/public/main.html`)
})
const PORT = 8000
app.listen(PORT, () =>{
console.log(`запуск на порте ${PORT}`)
})
*/
const express = require("express");
const cors = require("cors");
const dbConfig = require('./app/config/db.config');
const app = express();
var corsOptions = {
origin: "http://localhost:8081"
};
app.use(cors(corsOptions));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const db = require("./app/models");
const Role = db.role;
db.mongoose
.connect(`mongodb://${dbConfig.HOST}:${dbConfig.PORT}/${dbConfig.DB}`, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log("Успешное подключение к базе данных.");
initial();
})
.catch(err => {
console.error("Ошибка подключения к базе данных", err);
process.exit();
});
app.get("/", (req, res) => {
res.json({ message: "Приложение запущено." });
});
require('./app/routes/auth.routes')(app);
require('./app/routes/user.routes')(app);
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Сервер запущен на порте ${PORT}.`);
});
async function initial() {
try {
const count = await Role.estimatedDocumentCount();
if (count === 0) {
await Promise.all([
new Role({ name: "user" }).save(),
new Role({ name: "moderator" }).save(),
new Role({ name: "admin" }).save()
]);
console.log("Роли успешно добавлены.");
}
} catch (error) {
console.error("Ошибка:", error);
}
}