Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
101 lines (82 loc) · 2.66 KB
/
Copy pathserver.ts
File metadata and controls
101 lines (82 loc) · 2.66 KB
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
import express, { urlencoded } from "express";
import dotenv from "dotenv";
import cookieParser from "cookie-parser";
import cors from "cors";
import { Server } from "socket.io";
import { createServer } from "http";
dotenv.config();
// functions
import { connectToMongo } from "./services/connection"
import { socketSetup } from "./services/socket";
// routes
import { chatRoute } from "./routes/chat"
import { authRoute } from "./routes/auth";
import { roomRoute } from "./routes/room";
import { featureRoute } from "./routes/feature";
import { blogRoute } from "./routes/blog";
import { projectRoute } from "./routes/project";
// middlewares
import { allowOnlyAuthenticatedUser } from "./middlewares/auth";
const app = express();
const server = createServer(app);
const allowedOrigins = [
"http://localhost:5173",
"http://13.202.26.208",
"https://tnc.ayushz.me",
"https://ayushz.me",
"https://www.ayushz.me",
]
const io = new Server(server, {
cors: {
origin: (origin, callback) => {
if( !origin ) return callback(null, true); // for mobile app as they have no origin
if( allowedOrigins.includes(origin) ) {
callback(null, true);
} else {
console.log("Socket blocked by CORS:", origin);
callback(new Error("Not allowed by CORS"));
}
},
methods: ["GET", "POST"],
credentials: true,
}
});
app.set("io", io);
socketSetup(io);
const mongoUri = process.env.MONGO_URI;
if (!mongoUri) {
throw new Error(`MONGO_URI is not present in the environment variables`);
}
connectToMongo(mongoUri)
.then(() => console.log(`Connected to MongoDB`))
.catch((e: string) =>
console.log(`Error while connecting to database: ${e}`)
);
app.use(cors({
origin: (origin, callback) => {
if (!origin) return callback(null, true); // for mobile app as they have no origin
if (allowedOrigins.includes(origin)) {
callback(null, true);
} else {
console.log("API blocked by CORS:", origin);
callback(new Error('Not allowed by CORS'));
}
},
credentials: true,
}));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.get("/", allowOnlyAuthenticatedUser, (req, res) => {
return res
.status(200)
.json({ message: "Hey welcome to the TNC server" });
});
app.use('/api/chat', allowOnlyAuthenticatedUser, chatRoute);
app.use('/api/auth', authRoute);
app.use('/api/room', allowOnlyAuthenticatedUser, roomRoute);
app.use('/api/features', featureRoute);
app.use('/api/blog', blogRoute);
app.use('/api/project', projectRoute);
const PORT = process.env.PORT || 8000;
server.listen(PORT, () => console.log(`Server started on - http://localhost:${PORT}`));