-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
108 lines (68 loc) · 3.3 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
const express = require('express')
const app = express()
const mongoose = require('mongoose')
const dotenv = require('dotenv')
const cookieParser = require('cookie-parser')
const path = require('path')
const socketio = require('socket.io')
const http = require('http')
const server = http.createServer(app)
const io = socketio(server)
io.on('connection', (socket) => {
//console.log("User Connected...")
socket.on("notification-sent", (data) => {
io.emit("notification-received", data)
})
socket.on("notification-deleted-sent", (data) => {
io.emit("notification-deleted-recieved", data)
})
socket.on("new-operations", (data) => {
//console.log(data)
io.emit('new-remote-operations', data)
})
})
dotenv.config({ path: "./config.env" })
const { getAllDocs, createNewDocument, getSingleDoc, updateDoc, deleteDoc, doesDocExist, getSingleDocPopulated } = require('./controllers/docController')
const { signup, login, protect, isOwnerOrCollaborator, isLoggedIn, logout, acceptRequest, isCollaborator, isOwner, createAccessNotification, getOwner, getNotifications, deleteNotification, getUser, removeCollaborator, doesNotificationExist } = require('./controllers/authController')
const DB = process.env.DB.replace(
'<password>',
process.env.DB_PASS
)
// mongoose.connect('mongodb://localhost/test-db', { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true })
// .then(() => console.log('DB connection successful!'))
mongoose.connect(DB, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true })
.then(() => console.log('DB connection successful!'))
app.use(cookieParser())
app.use(express.json())
app.get('/api/docs', protect, getAllDocs)
app.get('/api/docs/:id', protect, isOwnerOrCollaborator, getSingleDoc)
app.get('/api/docs/populated/:id', protect, isOwner, getSingleDocPopulated)
app.post('/api/docs', protect, doesDocExist, createNewDocument)
app.patch('/api/docs/:id', protect, isOwnerOrCollaborator, doesDocExist, updateDoc)
app.patch('/api/docs/:id/removeCollaborator', protect, isOwner, removeCollaborator)
app.delete('/api/docs/:id', protect, isOwner, deleteDoc)
app.get('/api/docs/getOwner/:docId', protect, getOwner)
//app.post('/api/users/:userId/notifications/requestAccess', protect, createAccessNotification)
app.post('/api/users/signup', signup)
app.post('/api/users/login', login)
app.get('/api/users/getUser/:id', protect, getUser)
app.get('/api/users/isLoggedIn', isLoggedIn)
app.get('/api/users/logout', logout)
app.post('/api/users/notifications/requestAccess', protect, doesNotificationExist, createAccessNotification)
app.get('/api/users/notifications', protect, getNotifications)
app.post('/api/users/:docId', protect, acceptRequest)
app.delete('/api/notifications/:id', protect, deleteNotification)
if (process.env.NODE_ENV === "production") {
app.use(express.static('client/build'))
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
})
}
// const port = process.env.PORT || 8000
// app.listen(port, () => {
// console.log(`server started at port: ${port}`)
// })
const port = process.env.PORT || 8000
server.listen(port, () => {
console.log(`server started at port: ${port}`)
})