-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
152 lines (126 loc) · 3.82 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// since this is a demo unused files are deleted over the internet
// production wise you might want to just console.log unused file
// and manully delete them just to decrease server workload
// minor issues
// getAll send data in pagination format rather than standard format
import express from "express";
import mongoose from "mongoose";
import cors from "cors";
import dotenv from "dotenv";
import helmet from "helmet";
import path from "path";
import { fileURLToPath } from "url";
import authRoutes from "./routes/auth.js";
import userRoutes from "./routes/user.js";
import postRoutes from "./routes/post.js";
import shortRoutes from "./routes/short.js";
import miscRoutes from "./routes/misc.js";
import commentRoutes from "./routes/comment.js";
import cookieParser from "cookie-parser";
import socket from "./socket.js";
import { isProdMode, CLIENT_ORIGIN } from "./constants.js";
import { errHandler, validateCors } from "./utils/middlewares.js";
import timeout from "connect-timeout";
import { console500MSG } from "./utils/error.js";
import { demoUsers } from "./data.js";
import User from "./models/User.js";
import { createDemoDocAndComment } from "./utils/index.js";
// CONFIGURATIONS
const __dirname = path.dirname(fileURLToPath(import.meta.url));
dotenv.config();
const app = express();
const csp = [
"'self'",
"blob:",
"data:",
"https://apis.google.com",
"https://*.gstatic.com",
"https://*.googleapis.com",
"https://*.firebaseio.com",
"https://*.firebaseapp.com",
"https://*.firebase.com",
"https://*.googleusercontent.com"
];
// MIDDLEWARES
app.use(cors(validateCors));
app.use(
express.json({
limit: "200mb",
extended: true
})
);
app.use(express.urlencoded({ extended: true }));
// in production helmet causes Failed to load resource:
// net:: ERR_BLOCKED_BY_RESPONSE.NotSameOriginAfterDefaultedToSameOriginByCoep
app.use(
helmet({
contentSecurityPolicy: {
directives: {
"default-src": csp,
"script-src": csp,
"frame-src": csp,
"connect-src": csp,
"img-src": csp,
"media-src": csp
}
},
crossOriginEmbedderPolicy: false,
crossOriginResourcePolicy: { policy: "cross-origin" }
})
);
app.use(cookieParser());
// app.use(timeout("60s"));
app.use("/assets", express.static(path.join(__dirname, "public/assets")));
app.use(express.static("./client/build"));
// ROUTES
app.use("/api/auth", authRoutes);
app.use("/api/posts", postRoutes);
app.use("/api/users", userRoutes);
app.use("/api/shorts", shortRoutes);
app.use("/api/comments", commentRoutes);
app.use("/api", miscRoutes);
app.get("/*", function(req, res) {
res.sendFile(path.join(__dirname, "client/build/index.html"));
});
app.use(errHandler);
// MONGOOSE SETUP
mongoose.set("strictQuery", true);
mongoose.set("strictPopulate", false);
mongoose
.connect(process.env[isProdMode ? "MONGODB_PROD_URI" : "MONGODB_DEV_URI"], {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(async () => {
try {
const createUser = async u => {
if (!(await User.findById(u._id))) {
const user = await new User(u).save();
const posts = [];
const shorts = [];
for (let i = 0; i < 6; i++) {
posts.push(
createDemoDocAndComment(
user,
false,
i === 0 ? true : "likes only"
)
);
shorts.push(createDemoDocAndComment(user, true, false));
}
await Promise.all(posts);
await Promise.all(shorts);
}
};
const promises = [];
for (const u of demoUsers) {
promises.push(createUser(u));
}
await Promise.all(promises);
} catch (err) {
console.log(err.message);
} finally {
socket(app);
}
})
.catch(err => console500MSG(err, "DB_CONNECT_ERR"));