-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
134 lines (116 loc) · 2.93 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const express = require("express");
const bodyParser = require("body-parser");
const jsonfile = require("jsonfile");
const useragent = require("express-useragent");
const fs = require("fs");
const sse = require("./sse.js");
const mkdirp = require("mkdirp");
mkdirp("./dist/images/", (error) => {
if (error) {
console.error(error);
}
});
const app = express();
const connections = [];
app.set("port", (process.env.PORT || 9000));
app.use(express.static("./dist"));
app.use(bodyParser.json({
limit: "10mb"
}));
app.use(bodyParser.urlencoded({
limit: "10mb",
extended: true
}));
app.use(sse);
app.enable("trust proxy");
/**
* @name addToMessages
* @description
* adds the received message to the messages array and saves that array to
* messages.json
*
* @param {object} req
* server request object
* @param {object} res
* server response object
* @param {object} message
* message object
* @param {array} messages
* messages array
* @returns {promise}
* promise that resolves after messages.json is saved
*/
const addToMessages = (req, res, message, messages) => {
messages.push(Object.assign((() => {
delete message.chatTime;
if (message.image) {
message.image = `images/${message.image.name}`;
}
return message;
})(), {
ip: req.ip,
useragent: {
browser: {
name: useragent.parse(req.headers["user-agent"]).browser,
version: useragent.parse(req.headers["user-agent"]).version
},
platform: {
name: useragent.parse(req.headers["user-agent"]).platform,
os: useragent.parse(req.headers["user-agent"]).os
},
source: useragent.parse(req.headers["user-agent"]).source
}
}));
while (messages.length > 8) {
if (messages[0].image) {
fs.unlink(messages[0].image, () => {
messages.shift();
});
}
else {
messages.shift();
}
}
return jsonfile.writeFile("./dist/messages.json", messages, (writeFileError) => {
if (writeFileError) {
console.error(writeFileError);
}
connections.forEach((connection) => {
connection.sseSend(messages);
});
res.send("success");
});
};
jsonfile.readFile("./dist/messages.json", (readFileError, messages) => {
if (readFileError) {
console.error(readFileError);
}
const newMessages = messages;
app.post("/", (req, res) => {
const message = req.body;
if (message.image) {
fs.writeFile(`./dist/images/${message.image.name}`, message.image.file, "base64", (writeFileError) => {
if (writeFileError) {
console.error(writeFileError);
}
addToMessages(req, res, message, newMessages);
});
}
else {
addToMessages(req, res, message, newMessages);
}
});
});
app.get("/stream", (req, res) => {
jsonfile.readFile("./dist/messages.json", (readFileError, messages) => {
if (readFileError) {
console.error(readFileError);
}
res.sseSetup();
res.sseSend(messages);
connections.push(res);
});
});
app.listen(app.get("port"), () => {
console.log("App is running, server is listening on port ", app.get("port"));
});