-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
68 lines (59 loc) · 1.89 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
const express = require('express');
const cookieParser = require('cookie-parser');
const socketIo = require("socket.io");
const https = require('https')
const path = require('path')
const fs = require('fs')
const { connectDB, mongoose } = require('./utils/db');
require('dotenv').config({path: './utils/config.env'});
const app = express();
const port = process.env.PORT;
connectDB()
////////MIDDLEWARES/////////
// for parsing cookies
app.use(cookieParser());
//for parsing application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
// for parsing application/json
app.use(express.json());
////////ROUTES/////////
const authRoute = require("./routes/auth");
const postRoute = require("./routes/post");
const tagRoute = require("./routes/tag");
app.use("/api/auth", authRoute);
app.use("/api/posts", postRoute);
app.use("/api/tags", tagRoute);
///////SSL Server//////
const sslServer = https.createServer(
{
key: fs.readFileSync(path.join(__dirname, 'cert', 'key.pem')),
cert: fs.readFileSync(path.join(__dirname, 'cert', 'cert.pem')),
},
app
)
sslServer.listen(port , () => console.log(`Secure server running on :${port}`))
///////Socket IO///////
const io = socketIo(sslServer, {path: '/socket.io'});
const connection = mongoose.connection;
connection.once("open", () => {
console.log('MongoDB connection open');
const notificationChangeStream = connection.collection('notifications').watch();
notificationChangeStream.on('change', (change) => {
switch (change.operationType) {
case 'insert':
const notification = change.fullDocument;
console.log(notification);
};
});
});
io.on('connection', async (socket) => {
console.log(`User connected to socket.`);
socket.on('disconnect', async () => {
try {
console.log('user disconnected');
} catch(err) { console.log(err) }
});
socket.on("ping", async () => {
console.log('ping');
});
});