-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (80 loc) · 2.72 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
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
/* <--ProjApkInt-->
*
* Plik index.js jest głównym punktem całej aplikacji backendowej,
* gdyż wywołuje i uruchamia kontrolery, przypisuje im odpowiednie ścieżki
* oraz łączy się z bazą danych.
* Dodatkowo możliwe jest osadzenie tzw. middlewares, czyli metod pośredniczących
* w komunikacji pomiędzy użytkownikiem a kontrolerami
*
*/
const config = require("config")
const mongoose = require("mongoose")
const cors = require("cors")
const bodyParser = require("body-parser")
const express = require("express")
const login = require("./routes/login")
const register = require("./routes/register")
const track = require("./routes/track")
const sheet = require("./routes/sheet")
const enharmonicExcersise = require("./routes/enharmonicExcersise")
const intervalExcersise = require("./routes/intervalExcersise")
const offNoteExcersise = require("./routes/offNoteExcersise")
const voiceNoteExcersise = require("./routes/voiceNoteExcersise")
const app = express()
/* <--ProjApkInt-->
*
* W tym miejscu odbywa się sprawdzenie, czy plik konfiguracyjny zawiera klucz
* "jwtPrivateKey" oraz czy posiada on jakąś wartość. Klucz prywatny
* jest niezbędny do przeprowadzania autoryzacji oraz autentykacji.
*/
if (!config.get("jwtPrivateKey")) {
console.error("FATAL ERROR: jwtPrivateKey is not defined.")
process.exit(1)
}
/* <--ProjApkInt-->
*
* Tutaj aplikacja próbuje się połączyć z bazą danych. W przypadku niepowodzenia zostanie
* wyświetlony odpowiedni komunikat
*
*/
mongoose
.connect(config.get("db"), {
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
useUnifiedTopology: true
})
.then(() => console.log("Connected to MongoDB..."))
.catch(err => console.error("Could not connect to MongoDB...", err))
/* <--ProjApkInt-->
*
* Przykład uruchomienia funkcji middleware. Middleware express.json()
* parsuje zapytania posiadające body w formacie json do JavaScriptowych obiektów
*
*/
app.use(express.json())
app.use(cors())
app.use(
bodyParser.urlencoded({
extended: true
})
)
/* <--ProjApkInt-->
*
* Tutaj każdemu kontrolerowi przypisana jest jego ścieżka,
* za którą będzie odpowiedzialny. W poniższym przypadku
* kontroler "login" będzie odpowiedzialny za obsługę
* zapytań przychodzących pod ścieżkę "/api/login",
* np. "www.example.com/api/login"
*
*/
app.use("/api/login", login)
app.use("/api/register", register)
app.use("/api/tracks", track)
app.use("/api/sheets", sheet)
app.use("/api/enharmonic", enharmonicExcersise)
app.use("/api/interval", intervalExcersise)
app.use("/api/offNote", offNoteExcersise)
app.use("/api/voiceNote", voiceNoteExcersise)
const port = process.env.PORT || 3000
app.listen(port, () => console.log(`Listening on port ${port}...`))