-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
179 lines (141 loc) · 5.33 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const dotenv = require('dotenv').config();
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const colors = require('colors');
const cors = require('cors');
const corsOptions = {
origin: 'http://localhost:3000',
credentials: true,
optionSuccessStatus: 200
};
const stripe = require("stripe")("sk_test_51OA3YuHNsLfp0dKZBQsyFFPLXepbGkt9p5xZzd2Jzzj6zxLqUTY2DYF244qILCi0cfVjg37szrwdXZzin83e5ijm00X5eXuTnM");
const connectDB = require('./config/MongoDBConnection');
const adminRoutes = require('./routes/AdminRoutes');
const DeleteModelRecords = require('./config/DeleteAllRecords');
const doctorRoutes = require('./routes/DoctorRoutes');
const patientRoutes = require('./routes/PatientRoutes');
const genericRoutes = require('./routes/GenericRoutes');
const chatRoutes = require('./routes/ChatsRoutes');
// Connect to MongoDB
connectDB().then(r => console.log("Connected to MongoDB 200 OK".bgGreen.bold));
//Start Express server
const app = express();
const Port = process.env.PORT;
const http = require('http');
const server = http.createServer(app);
const io = require('socket.io')(server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
}
});
app.use(cors(corsOptions));
const {putSocket, getSocket, joinSocket} = require('./config/socket');
// server.listen(5000);
io.on("connection", (socket) => {
socket.on("iAmReady", (username) => {
console.log("iAmReady: " + username);
putSocket(username, socket.id);
socket.emit("me", socket.id);
});
socket.on("iWantToJoin", async () => {
console.log("iWantToJoin");
await joinSocket(socket);
});
socket.on("disconnect", () => {
socket.broadcast.emit("callEnded")
});
socket.on("callUser", async ({ userToCall, signalData, from, name }) => {
const idToCall = await getSocket(userToCall);
console.log("callUser: " + userToCall + " with socketID: " + idToCall);
io.to(idToCall).emit("callUser", { signal: signalData, from, name });
});
socket.on("answerCall", (data) => {
console.log("call answered, " + data.to + " is the caller");
io.to(data.to).emit("callAccepted", data.signal)
});
socket.on("join chat", (room) => {
socket.join(room);
console.log("User Joined Room: " + room);
});
socket.on("newMessage", async({message, receiver}) => {
const socketID = await getSocket(receiver);
console.log("newMessage: " + message.content + " to " + receiver + " with socketID: " + socketID);
io.to(socketID).emit("newMessage", message);
});
socket.on("newMessagePharmacy", async({message, receiver, sendingToPharmacy}) => {
const socketID = await getSocket(receiver);
console.log("newMessagePharmacy: " + message.content + " to " + receiver + " with socketID: " + socketID + " sendingToPharmacy: " + sendingToPharmacy);
const room = receiver + " room";
if(sendingToPharmacy){
io.to(room).emit("newMessagePharmacy", message);
}else{
io.to(socketID).to(room).emit("newMessagePharmacy", message);
}
});
});
app.use(express.static("public"));
app.use(express.json());
//DeleteModelRecords.deleteAllRecords(); //uncomment this line to delete all records from a specific model
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
server.listen(Port);
console.log("Server running at http://localhost:" + process.env.PORT + "/");
app.set('view engine', 'ejs');
// const corsOptions = {
// origin: 'http://example.com', // Replace with your frontend's URL
// methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
// credentials: true, // Enable credentials (e.g., cookies, authorization headers)
// };
//
// app.use(cors(corsOptions));
// routes
app.use('/admin', adminRoutes);
app.use('/doctor', doctorRoutes);
app.use('/patient', patientRoutes);
app.use('/', genericRoutes);
app.use('/chat', chatRoutes);
app.post("/package/create-payment-intent", async (req, res) => {
// const { items } = req.body;
const card = req.body.card;
console.log("in the package payment intent");
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: 100,
currency: "usd",
// In the latest version of the API, specifying the `automatic_payment_methods` parameter is optional because Stripe enables its functionality by default.
automatic_payment_methods: {
enabled: true,
}
});
res.send({
clientSecret: paymentIntent.client_secret,
});
});
app.post("/create-payment-intent", async (req, res) => {
// const { items } = req.body;
const card = req.body.card;
console.log("in the payment intent");
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: 100,
currency: "usd",
// In the latest version of the API, specifying the `automatic_payment_methods` parameter is optional because Stripe enables its functionality by default.
automatic_payment_methods: {
enabled: true,
}
});
res.send({
clientSecret: paymentIntent.client_secret,
});
});
module.exports = app;