-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
59 lines (41 loc) · 1.29 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
process.on('uncaughtException',(err)=>{
console.log('error',err)
})
import express from 'express'
import dotenv from 'dotenv'
import morgan from 'morgan'
import { dbconnection } from './databases/dbconnection.js'
import { init } from './src/index.routes.js'
import cors from 'cors'
import { Server } from "socket.io";
import { watchFoundChanges } from './src/modules/notifications/foundChiWatch.controller.js'
import { adminNotifModel } from './databases/models/adminNotifi.model.js'
dotenv.config()
const app = express()
const port = 3000
watchFoundChanges()
//middleware
app.use(cors())
app.use(express.json())
app.use(morgan('dev'))
app.use(express.static('uploads'))
init(app)
dbconnection()
let server = app.listen(process.env.PORT || port, () => console.log(`Example app listening on port ${port}!`))
process.on('unhandledRejection',(err)=>{
console.log('errrrrrr',err)
})
const io = new Server(server ,{
cors:"*"
});
io.on('connection', (socket) => {
console.log(socket.id);
console.log('a user connected');
socket.on('requestNotificationCount', async () => {
const count = await adminNotifModel.find();
socket.emit('notificationCount', count.length);
});
socket.on('disconnect', () => {
console.log('a user disconnected');
});
});