-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
79 lines (66 loc) · 2.15 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
71
72
73
74
75
76
77
78
79
import express from "express";
import cors from "cors";
import http from "http";
import {Server as SocketIOServer} from "socket.io";
import swaggerJsDoc from "swagger-jsdoc";
import swaggerUi from "swagger-ui-express";
import { getUseInfo } from "./routes/userInfo.js";
import { getChatMessages, postChatMessage } from "./routes/chatmessages.js";
import { registerUser } from "./routes/registerUser.js";
import { POST } from "./routes/loginUser.js";
import { createOrFindChatChannel } from "./routes/Chatchannels.js";
import { getAllUsers } from "./routes/Getallusers.js";
const app = express();
const server = http.createServer(app);
const io = new SocketIOServer(server);
const PORT = 3001;
app.use((req, res, next) => {
req.io = io;
next();
});
const swaggerOptions = {
definition: {
openapi: "3.0.0",
info: {
title: "Chattie API",
version: "1.0.0",
description: "A simple Express NFT API",
},
servers: [
{
url: "http://localhost:3001",
},
{ url: "https://nftapis.onrender.com" },
],
},
apis: ["./routes/*.js"], // path to your route files
};
const swaggerDocs = swaggerJsDoc(swaggerOptions);
// Use CORS with options
app.use(cors({
origin: "*", // Specify allowed origins
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true, // Allow cookies
allowedHeaders: "Content-Type,Authorization"
}));
// Enable pre-flight across-the-board
app.options('*', cors()); // include before other routes
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocs));
app.use(express.json());
app.post("/register", registerUser);
app.post("/login", POST);
app.post("/chat/channel", createOrFindChatChannel)
app.post('/chat/message', postChatMessage);
app.get('/chat/messages/:chatChannelId', getChatMessages);
app.get("/userInfo", getUseInfo);
app.get("/users", getAllUsers);
io.on("connection", (socket) => {
console.log("User connected", socket.id);
socket.on('joinRoom', (chatChannelId) => {
socket.join(chatChannelId);
console.log(`User ${socket.id} joined room ${chatChannelId}`);
});
});
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});