-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
92 lines (76 loc) · 2.3 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
80
81
82
83
84
85
86
87
88
89
90
91
92
import express, { urlencoded } from "express";
import { createServer } from "http";
import { Server } from "socket.io";
import routes from "./src/public/Routes/routes.js";
import mainFunc from "./src/public/Controller/functions.js";
import "./src/mongo/schema.js";
import fileUpload from "express-fileupload";
// const fileUpload = require('fileUpload');
import messageController from "./src/public/Controller/message.controller.js";
import editor from "./src/public/Controller/userEdit.js";
import cors from "cors";
import fs from "fs";
import { v4 as uuid } from "uuid";
import { fileTypeFromBuffer } from "file-type";
let {sentMessage}=messageController
let { format } = mainFunc;
let { app: Router } = routes
let {setActive}= editor
const app = express();
const httpServer = createServer(app);
app.use(cors())
// app.use(express.urlencoded())
app.use(express.static(format("src", "front")));
app.use(express.static(format("db")));
app.use('/img',express.static(format("db",'messages')));
app.use(express.json());
app.use(express.text());
app.use(fileUpload());
console.log('Request')
app.use(Router);
const io = new Server(httpServer, {});
io.on("connection", (socket) => {
// console.log('connected');
socket.on('active', async (data) => {
let res = await setActive(data.token)
})
socket.on("message1", async (formData) => {
if (formData.img) {
sentMessage(formData)
console.log("img sended");
} else {
sentMessage(formData)
console.log("message sended");
}
// console.log(formData);
if (formData.img) {
let nameImg = uuid();
const fileInfo = await fileTypeFromBuffer(formData.img);
// console.log(fileInfo);
let imgPath = `${nameImg}.${fileInfo.ext}`;
io.emit(
"hello",
{
path: "/img/" + imgPath,
type: imgPath ? "img" : "",
text: formData.message,
username: formData.senderUsername,
},
imgPath
);
} else {
io.emit(
"hello",
{
text: formData.message,
username: formData.senderUsername,
}
);
}
});
socket.on("disconnect", async(data) => {
let token = socket.handshake.query.token;
await setActive(token,new Date())
});
});
httpServer.listen(3000, console.log("Running.."));