-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
77 lines (65 loc) · 1.99 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
require('dotenv').config()
const express = require('express')
const path = require('path')
const mongoose = require('mongoose')
const cors = require('cors')
const fileUpload = require('express-fileupload')
const cookieParser = require('cookie-parser')
const app = express()
app.use(express.json())
app.use(cookieParser())
app.use(cors({
credentials: true //Để bật cookie HTTP qua CORS
}))
app.use(fileUpload({
useTempFiles: true
}))
const http = require('http').createServer(app)
const io = require('socket.io')(http, {
cors: {
origin: '*'
}
})
//socketIO
const NEW_CHAT_MESSAGE_EVENT = "newChatMessage";
io.on("connection", (socket) => {
// Join a conversation
const { roomId } = socket.handshake.query;
socket.join(roomId);
// Listen for new messages
socket.on(NEW_CHAT_MESSAGE_EVENT, (data) => {
io.in(roomId).emit(NEW_CHAT_MESSAGE_EVENT, data);
});
// Leave the room if the user closes the socket
socket.on("disconnect", () => {
socket.leave(roomId);
});
});
//Routes
app.use('/user', require('./routes/UserRouter'))
app.use('/doctor', require('./routes/DoctorRouter'))
app.use('/api', require('./routes/UploadToCloudinary')) //Cloudinary
app.use('/city', require('./routes/CityRouter'))
app.use('/district', require('./routes/DistrictRouter'))
app.use('/form', require('./routes/MedicineFormRouter'))
app.use('/medicine', require('./routes/MedicineRouter'))
app.use('/health', require('./routes/HealthDeclarationRouter'))
app.use('/pharmacy', require('./routes/PharmacyRouter'))
//connect to mongoDB
const URL = process.env.MONGO_URL
mongoose.connect(URL, {
useCreateIndex: true,
useFindAndModify: false,
useNewUrlParser: true,
useUnifiedTopology: true
}, err => {
if (err) throw err;
console.log('Connected to MongoDB.');
})
app.get('/', (req, res) => {
res.json({ msg: "ON AIR!!!" })
})
const PORT = process.env.PORT || 3000
http.listen(PORT, '127.0.0.1', () => {
console.log('Server is running on', PORT);
})