forked from Greg001100/harmony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
101 lines (84 loc) · 2.88 KB
/
app.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
const express = require('express');
const morgan = require('morgan');
const path = require('path')
const db = require('./models')
const { ValidationError } = require("sequelize");
const cors = require("cors");
const bodyParser = require('body-parser');
const { createServer } = require('http');
const WebSocket = require('ws')
const {Message, User} = db;
const usersRouter = require("./routes/users")
const serverRouter = require("./routes/servers")
const app = express();
app.use(morgan('dev'));
app.use(cors({ origin: true }));
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use('/', usersRouter)
app.use('/servers', serverRouter)
const port = Number.parseInt(process.env.PORT, 10) || 8081;
if (process.env.NODE_ENV === 'production') {
app.use(express.static('harmony_frontend/build'));
app.get('/', (req, res) => {
res.sendFile(path.resolve(__dirname, 'harmony_frontend', 'build', 'index.html'));
});
}
app.use((req, res, next) => {
const err = new Error("The requested resource couldn't be found.");
err.errors = ["The requested resource couldn't be found."];
err.status = 404;
next(err);
});
app.use((err, req, res, next) => {
if (err instanceof ValidationError) {
err.errors = err.errors.map((e) => e.message);
err.title = "Sequelize Error";
}
next(err);
});
// app.use((err, req, res, next) => {
// res.status(err.status || 500);
// const isProduction = environment === "production";
// res.json({
// title: err.title || "Server Error",
// message: err.message,
// errors: err.errors,
// stack: isProduction ? null : err.stack,
// });
// });
const server = createServer(app);
const wss = new WebSocket.Server({server})
wss.on('connection', (ws) => {
ws.on('message', async (jsonData) => {
console.log(`Processing incoming message ${jsonData}...`);
const message = JSON.parse(jsonData);
const {value, userId, channelId} = message.data
const recMessage = await Message.create({value, userId, channelId})
const chatMessage = await Message.findByPk(recMessage.id, {include:[User]})
const addChatMessage = {
type: 'add-chat-message',
data: chatMessage,
};
const jsonAddChatMessage = JSON.stringify(addChatMessage);
console.log(`Sending message ${jsonAddChatMessage}...`);
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(jsonAddChatMessage);
}
});
});
ws.on('close', (e) => {
console.log(e);
});
});
db.sequelize
.authenticate()
.then(() => {
console.log("Database connection success! Sequelize is ready to use...");
server.listen(port, () => console.log(`Listening on port ${port}...`));
})
.catch((err) => {
console.log("Database connection failure.");
console.error(err);
});