-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
117 lines (92 loc) · 2.92 KB
/
server.js
File metadata and controls
117 lines (92 loc) · 2.92 KB
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
117
import http from 'http'
import express from 'express'
import { Server } from 'socket.io'
import mongoose from 'mongoose'
import Pixel from './models/Pixel.js'
const app = express()
const server = http.createServer(app)
const io = new Server(server)
import dotenv from 'dotenv'
dotenv.config()
import morgan from 'morgan'
import 'express-async-errors'
// db and authentication
import connectDB from './db/connect.js'
// routers
import authRouter from './routes/authRoute.js'
import pixelmapRouter from './routes/pixelmapRoute.js'
import userRouter from './routes/userRoute.js'
// middleware
import notFoundMiddleware from './middleware/not-found.js'
import errorHandlerMiddeware from './middleware/error-handler.js'
import authenticateUser from './middleware/auth.js'
if (process.env.NODE_ENV !== 'Production') {
app.use(morgan('dev'))
}
app.use(express.json())
// app.get('/', (req, res) => {
// res.send('Welcome!')
// })
app.use('/api/v1/auth', authRouter)
app.use('/api/v1/pixelmap', authenticateUser, pixelmapRouter)
// app.use('/api/v1/pixelmap', pixelmapRouter)
app.use('/api/v1/user', authenticateUser, userRouter)
app.use(notFoundMiddleware)
app.use(errorHandlerMiddeware)
const PORT = process.env.PORT || 5500
io.of('/api/v1/socket').on('connection', (socket) => {
console.log('socket.io: User connected: ', socket.id)
socket.on('disconnect', () => {
console.log('socket.io: User disconnected: ', socket.id)
})
})
const start = async () => {
try {
connectDB(process.env.MONGO_URL)
const connection = mongoose.connection
connection.once('open', () => {
console.log('MongoDB database connected')
console.log('Setting change streams')
const pixelmapChangeStream = connection.collection('pixels').watch()
pixelmapChangeStream.on('change', async (change) => {
switch (change.operationType) {
case 'insert':
var pixel = {
row: change.fullDocument.row,
color: change.fullDocument.color,
state: change.fullDocument.state,
}
io.of('/api/v1/socket').emit('newPixel', pixel)
break
case 'update':
console.log(change.documentKey._id.toString())
const pixelexist = await Pixel.findOne({
_id: change.documentKey._id.toString(),
})
console.log(pixelexist)
io.of('/api/v1/socket').emit('newPixel', pixelexist)
break
default:
break
}
})
})
server.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}...`)
})
} catch (error) {
console.log(error)
}
}
start()
import CronJob from 'node-cron'
import User from './models/User.js'
CronJob.schedule('0 0 */23 * * *', async () => {
const users = await User.find()
for (const user of users) {
user.point += 1
await user.save()
}
const d = new Date()
console.log('Log: ', d)
})