-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
120 lines (86 loc) · 3.46 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const express = require("express");
const mongoose = require("mongoose");
const session = require('express-session');
const ejsMate = require('ejs-mate');
const dotenv = require('dotenv');
const morgan = require('morgan');
const flash = require('connect-flash');
const cors = require('cors');
const app = express();
const port = process.env.PORT || 5000;
if (process.env.NODE_ENV !== 'production') {
dotenv.config();
}
const config = require("./config");
const signUpRouter = require("./routers/signUpRouter");
const signInRouter = require("./routers/signInRouter");
const authorRouter = require("./routers/authorRouter");
const poemRouter = require("./routers/poemRouter");
const remainControllers = require("./controllers/remainControllers");
const signOutController = require("./controllers/signOutController");
const annotationRouter = require("./routers/annotationRouter");
const profileRouter = require("./routers/profileRouter");
const helpers = require("./helpers");
const AppError = require("./AppError");
const User = require("./models/user");
const Annotation = require("./models/annotation");
const Poem = require("./models/poem");
const Author = require("./models/author");
const healthCheckRouter = require("./routers/healthCheckRouter");
const statisticsRouter = require("./routers/statisticsRouter");
// Enabled to fetch requests /api/statistics
// But involves some security issues ???
app.use(cors({
origin: [`http://localhost:${port}`, `http://aqyndar.herokuapp.com`],
}));
app.use("/static", express.static(__dirname + "/public"));
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.engine('ejs', ejsMate);
app.set("view engine", "ejs");
app.listen(port, () => console.log(`Server is running on port ${port}`));
mongoose.connect(config.mongoURI, {useNewUrlParser: true, useUnifiedTopology: true})
.then((result) => console.log(`Connected to MongoDB ${result.connection.host}`))
.catch((err) => console.log(err));
app.use(session(config.sessionConfig));
app.use(morgan('dev'));
app.use(flash());
app.get("/", remainControllers.displayMainPage);
app.get("/about", remainControllers.displayAboutPage);
app.get('/faq', remainControllers.displayFAQPage);
app.get("/sign_out", helpers.isAuth, signOutController.signOut);
app.use('/profile', profileRouter);
app.use("/sign_up", signUpRouter);
app.use("/sign_in", signInRouter);
app.use("/authors", authorRouter);
app.use("/poems", poemRouter);
app.use("/annotations", annotationRouter);
app.use("/test", healthCheckRouter);
app.use('/api', statisticsRouter);
app.get('/favicon.ico', (req, res, next) => res.status(204));
// Only for development
app.get("/error", (req, res, next) => {
console.log("Starting /error route");
const object = {};
object.fly();
console.log('Ending /error route');
res.send("No errors here");
});
// Not found handler
app.use((req, res, next) => {
const err = new AppError(`Бұл веб-парақша ескірген, яғни жойылған немесе мүлдем болмаған.`, 404);
next(err);
});
// Error handling
app.use((err, req, res, next) => {
if (!err instanceof AppError) {
console.log(err);
}
const {status = 500, message = "Күтпеген қате. [email protected]ге хабарласыңыз."} = err;
res.status(status).render('error', {
title: 'Қате',
isLogged: Boolean(req.session.user_id),
errorMessage: message,
statusCode: status,
});
});