-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
123 lines (102 loc) · 2.89 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
121
122
123
// ℹ️ Gets access to environment variables/settings
// https://www.npmjs.com/package/dotenv
require("dotenv/config");
// ℹ️ Connects to the database
require("./db");
// Handles http requests (express is node js framework)
// https://www.npmjs.com/package/express
const express = require("express");
const isAdmin = require("./utils/is_admin");
// Handles the handlebars
// https://www.npmjs.com/package/hbs
const hbs = require("hbs");
hbs.registerHelper("language", function (lang) {
switch (lang) {
case "css":
return "language-css";
break;
case "html":
return "language-html";
break;
case "js":
return "language-js";
break;
case "py":
return "language-py";
break;
default:
return "language-html";
break;
}
});
hbs.registerHelper("count", (arrayToCount)=> {
return arrayToCount.length;
});
hbs.registerHelper("isOwner", (ownerId)=>{
if(ownerId._id.toString() === app.locals.user._id || isAdmin(app.locals.user)){
return "flex"
}else{
return "none"
}
})
hbs.registerHelper("isOwnerMsg", (ownerId)=>{
if(ownerId._id.toString() === app.locals.user._id || isAdmin(app.locals.user)){
return ""
}else{
return "margin-left"
}
})
hbs.registerHelper("isAdminHBS", () =>{
if (app.locals.user.role === "admin"){
return "flex"
}else{
return "none"
}
})
hbs.registerHelper("follow", (followersArray) =>{
if (followersArray.includes(app.locals.user._id)){
return "UnFollow"
}else{
return "Follow"
}
})
hbs.registerHelper("userLiked", (likeArray, empty)=>{
if (likeArray.includes(app.locals.user._id)){
if (empty){
return "in-line"
} else{
return "none"
}
}else{
if (empty){
return "none"
} else{
return "in-line"
}
}
})
hbs.registerHelper("chatColor", (fromUser)=>{
if (app.locals.user._id === fromUser._id.toString()){
return "border-chat-box"
} else{
return "border-chat-box-to"
}
})
hbs.registerHelper("cuttingdata", (originalData)=>{
return originalData.toString().split(" ").slice(1, 5).join(" ")
})
const app = express();
// ℹ️ This function is getting exported from the config folder. It runs most pieces of middleware
require("./config")(app);
const capitalized = require("./utils/capitalized");
const projectName = "codeGram";
app.locals.appTitle = `${capitalized(projectName)} created with IronLauncher`;
// 👇 Start handling routes here
const index = require("./routes/index.routes");
app.use("/", index);
const authRoutes = require("./routes/auth.routes");
const UserModel = require("./models/User.model");
app.use("/auth", authRoutes);
// ❗ To handle errors. Routes that don't exist or errors that you handle in specific routes
require("./error-handling")(app);
module.exports = app;