-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathapp.js
116 lines (86 loc) · 2.84 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';
const express = require("express");
const bodyParser = require("body-parser");
const mongo = require("mongodb");
const mongoose = require("mongoose");
const passport = require("passport");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const app = express();
const router = express.Router();
const mockData = require("./server/mock-data");
const config = require("./server/config/config");
const SerialAuthenticator = require("./server/auth/index");
const User = require("./server/models/user.model");
const Message = require("./server/models/message.model");
const authRoutes = require("./server/routers/auth-routers")(passport);
const userRoutes = require("./server/routers/user-routers")();
const db = "mongodb://test:[email protected]:19988/skypeclone";
//const db = config.DB_Connection.URL;
const port = process.env.PORT || 3001;
mongoose.Promise = global.Promise;
mongoose.connection.openUri(db);
app.use(express.static('public'))
//Enable all CORS requests
app.use(cors());
app.use(router);
app.use("/auth", authRoutes); // login/out authentication routes
app.use("/user", userRoutes); // user authentication
router.use(cookieParser());
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: true }));
router.use(
require("express-session")({
secret: config.SESSION_SECRET.SECRET_KEY,
resave: true,
saveUninitialized: true
})
);
router.use(passport.initialize());
router.use(passport.session());
// passport de/serialize and local strategy
SerialAuthenticator(passport);
router.get("/", (req, res, next) => res.send("Home"));
// ErrorHandler, pass errors to the next function
app.use((err, req, res, next) => {
res.send(err);
next();
});
const server = app.listen(port, () => {
console.log("Server started on port....." + port);
});
const io = require('socket.io').listen(server);
io.sockets.on('connection', socket => {
/*const sessionid = socket.id;
console.log("Socket Connected: %s", sessionid);
socket.on('message', body => {
socket.broadcast.emit('message', {
body,
id: sessionid,
})
})
socket.on('joinRoom', (roomInfo)=>{
console.log('joining room', roomInfo);
//Save into chatmodel.
socket.join(roomInfo.roomID);
});
*/
socket.on('joinRoom', (chatID) => {
console.log('joining room', chatID);
//Save into chatmodel.
socket.join(chatID);
});
socket.on('privateMessage', function (data) {
console.log('chatMessages', data);
Message.create({
chatID: data.chatID,
userID: data.userID,
message: data.messageBody
})
.then(data => console.log(data))
.catch(err => console.log(err));
socket.broadcast.to(data.chatID).emit('chatMessages', data);
});
});
console.log(`SkypeClone's Socket server is running at port 8080...
Wait! for a while to start client side server.`);