-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (41 loc) · 1.83 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
const http = require("http");
const logger = require("morgan");
const express = require("express");
const app = express();
const server = http.createServer(app);
const mongoose = require("mongoose");
require("dotenv/config.js");
const { regModel } = require("./models/regModel");
const socketController = require("./controllers/socket.controller");
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// Socket io implementation start
const io = require("socket.io")(server);
io.on("connection", async (socket) => {
console.log("::::::::::::::::::NEW CONNECTION:::::::::::::::::");
socket.on("login", (data) => socketController.handleLogin(data, socket, io));
socket.on("message", (data) => socketController.handleMessage(data, socket, io));
socket.on("room", (data) => socketController.handleMessageRoom(data, socket, io));
socket.on("chat", (data) => socketController.handleChat(data, socket, io));
socket.on("deletechat", (data) => socketController.handleDeleteChat(data, socket, io));
socket.on("seen", (data) => socketController.handleSeen(data, socket, io));
socket.on("disconnect", async () => await regModel.updateOne({ socketId: socket.id }, { $set: { socketId: null } }));
});
// Socket io implementation finish
// middlewares
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(logger("dev"));
const PORT = process.env.PORT || 3001;
// routes
const index_routes = require("./routes/index.routes");
app.use("/api/v1", index_routes);
app.get("/", (req, res) => {
return res.send("running");
})
// db connection
mongoose.connect("mongodb://0.0.0.0:27017/userData").then(() => console.log("MongoDb : START"));
server.listen(PORT, console.log(`Server @${PORT}`));