-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
151 lines (138 loc) · 3.33 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
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
const express = require("express");
const colors = require("colors");
const morgan = require("morgan");
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const uniqueID = require("short-unique-id");
const cors = require('cors');
dotenv.config();
const app = express();
// middlewares
app.use(express.json());
app.use(morgan("dev"));
app.use(bodyParser.urlencoded({extended : true}));
app.use(express.urlencoded({extended : true}));
app.use(cors());
//defining schema
const userAuth = {
username : {
type : String,
required : true,
unique : true
},
password : {
type : String,
required : true
}
};
// Define a regular expression to validate phone numbers (example format)
const phoneRegex = /^[0-9]{10}$/;
const patientSchema = {
name: {
type: String,
required: true
},
dob: {
type: String,
required: true
},
gender: {
type: String,
required: true
},
phoneNumber: {
type: String,
required: true,
},
symptoms: {
type: [String],
required: true
},
bloodGroup: {
type: String,
required: true
},
height: {
type: String,
required: true
},
weight: {
type: String,
required: true
},
dbID : {
type : String,
required : true
}
};
//connecting to server
mongoose.connect('mongodb+srv://shubhamgupta9454666551:[email protected]/MediSync').then(()=>{
console.log("mongo db connected");
}).catch((error)=>{
console.log("Error occured",error);
})
//creating collections
const user = mongoose.model("user",userAuth);
const patientData = mongoose.model("patientData",patientSchema);
//routes
app.get("/", (req, res) => {
res.status(200).send({
message: "Server runninng",
});
});
app.post("/signup",async (req,res)=>{
console.log(req.body);
const userName = req.body.username;
const passWord = req.body.password;
const date = new Date();
const newUser = new user({
username : userName,
password : passWord
});
await newUser.save();
console.log( newUser );
res.status(200).json({code : 1,message : "success"});
});
app.post("/login",async (req,res)=>{
const eUsername = req.body.username;
const ePassword = req.body.password;
const resQuery = await user.find({username : eUsername});
if(resQuery.length == 0){
res.status(200).json({code : 0,message : "user not found"});
}
else{
if(resQuery[0].password === ePassword){
res.status(200).json({code : 1,message : "User found"});
}
else{
res.status(200).json({code : 2,message : "Incorrect username or password"});
}
}
});
app.post("/patient-dashboard",async (req,res)=>{
const uid = new uniqueID({length : 10});
const newPatient = new patientData({
name: req.body.Name,
dob: req.body.DOB,
gender: req.body.Gender,
phoneNumber : req.body.Phone_Number,
symptoms: req.body.Symptoms,
bloodGroup: req.body.Blood_Group,
height : req.body.Height,
weight : req.body.Weight,
dbID : uid.rnd()
});
await newPatient.save();
console.log(newPatient);
res.status(200).json({code : 1,message : "success"});
});
//port
const port = process.env.PORT || 8080 ;
//listen port
app.listen(port, () => {
console.log(
`Server running in ${process.env.NODE_MODE} Mode on port ${process.env.PORT}`
.bgCyan.white
);
});