-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
112 lines (92 loc) · 2.7 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
import express from "express";
import passport from "passport";
import mongoose from "mongoose";
import bodyParser from "body-parser";
import morgan from "morgan";
import chalk from "chalk";
import socketIo from "socket.io";
import http from "http";
import User from "./models/User";
import Message from "./models/Message";
import auth from "./routes/auth";
import initPassport from "./passport";
const app = express();
const server = http.Server(app);
process.on("unhandledRejection", function(err) {
console.log(err.stack);
process.exit(1);
});
app.use(
morgan((tokens, req, res) => {
let method;
if (tokens.status(req, res) < 400) {
method = chalk.green(tokens.method(req, res));
} else {
method = chalk.red(tokens.method(req, res));
}
return (
method +
" " +
chalk.white(tokens.url(req, res)) +
" " +
tokens.status(req, res) +
" " +
chalk.red(tokens["response-time"](req, res))
);
}),
);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
mongoose.connect("mongodb://localhost/chatApp").then(() => console.log("MongoDB started"));
//Passport middleware
app.use(passport.initialize());
//Passport config
initPassport(passport);
app.use("/auth", auth);
// app.use((req, res, next) => {
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Headers", "X-Requested-With");
// res.header("Access-Control-Allow-Headers", "Content-Type");
// res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS");
// next();
// });
const exServer = app.listen(5000, () => console.log("Server live on 5000..."));
// Socket
import SocketIo from "socket.io";
// const io = SocketIo(server);
const io = socketIo.listen(exServer);
let activeUsers = [];
io.set("origins", "*:*");
io.on("connection", client => {
console.log("Client connected ", client.id);
client.on("ADD_USER_TO_LIST", ({ user }) => {
if (activeUsers.indexOf(user) > -1) {
activeUsers.splice(activeUsers.indexOf(user), 1);
}
activeUsers.push(user);
io.emit("UPDATE_USER_LIST", activeUsers);
});
client.on("SEND_MESSAGE", async data => {
const message = new Message(data);
await message.save();
io.emit("RECEIVE_MESSAGE", message);
});
client.on("GET_MESSAGES", async () => {
try {
const messages = await Message.find({});
io.emit("SET_MESSAGES", messages);
} catch (err) {
io.emit("SET_MESSAGES", []);
console.log(err);
}
});
client.on("DELETE_USER_FROM_LIST", ({ user }) => {
const index = activeUsers.indexOf(user);
activeUsers.splice(index, 1);
io.emit("UPDATE_USER_LIST", activeUsers);
});
console.log(activeUsers);
client.on("disconnect", () => {
console.log("client disconnect...", client.id);
});
});