-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
70 lines (56 loc) · 1.71 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
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const helmet = require("helmet");
const morgan = require("morgan");
const router = require("./src/Routes/index");
const cors = require("cors");
const { connectSocketIo } = require("./src/socket");
dotenv.config();
const PORT = process.env.PORT || 2001;
const mongoURI = process.env.MONGOURI;
const allowedOrigin = process.env.CORS_ORIGIN.split(",").map(
(origin) => origin
);
app.use(
cors({
origin: allowedOrigin,
})
);
app.use(express.json());
app.use(helmet());
//app.use(morgan("short"));
async function connectMongoDB() {
try {
const { connections } = await mongoose.connect(mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const server = app.listen(PORT, () => {
console.log(
`Server listening @ PORT ${PORT} && MongoDB running @ PORT ${connections[0].port})`
);
});
const onlineUsers = {};
connectSocketIo(server, allowedOrigin, onlineUsers);
} catch (error) {
console.log("error inside connectMongoDB", error);
}
}
app.use(express.static(__dirname + '/public'));
app.get("/welcome", (req, res) => {
console.log("allowed origin", allowedOrigin);
res.send("Welcome server is running");
});
app.use("/userAuth", router.authUser);
app.use("/follow", router.followUserRouter);
app.use("/post", router.postRouter);
app.use("/user", router.userRouters);
app.use("/chat", router.chatRouter);
app.use("/message", router.messageRouter);
app.use("/notification", router.notificationRouter);
app.use("*", (req, res) => {
res.status(404).sendFile(__dirname + '/public/NotFoundPage.html');
})
connectMongoDB();