-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
118 lines (84 loc) · 3.05 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
const express = require("express")
const app = express()
const mongoose = require("mongoose")
const cors= require("cors")
const Patient= require("./models/Patient")
const Hospital= require("./models/Hospital")
app.use (express.json())
app.use(cors())
main().then(()=>{
console.log("connected to mongoDB")
}).catch((err)=>{
console.log(err)
})
async function main(){
await mongoose.connect("mongodb://127.0.0.1:27017/SIH")
}
app.post('/signup', async (req, res) => {
try {
const { name, phoneNumber, age, password } = req.body;
// Create a new patient instance
const newPatient = new Patient({ name, phoneNumber, age, password });
// Save the patient to the database
await newPatient.save();
res.status(201).send('User signed up successfully');
} catch (error) {
res.status(500).send('Error signing up user: ' + error.message);
}
});
app.post('/login', async (req, res) => {
try {
const { phoneNumber, password } = req.body;
// Find the patient by phoneNumber
const patient = await Patient.findOne({ phoneNumber });
if (!patient) {
return res.status(401).send('Invalid phone number or password');
}
// Compare the provided password with the hashed password
const isMatch = await patient.comparePassword(password);
if (!isMatch) {
return res.status(401).send('Invalid phone number or password');
}
// Respond with a success message or token
res.status(200).send('Login successful');
} catch (error) {
res.status(500).send('Error logging in user: ' + error.message);
}
});
app.post('/signup-hospital', async (req, res) => {
try {
const { name, phoneNumber, password } = req.body;
// Create a new hospital instance
const newHospital = new Hospital({ name, phoneNumber, password });
// Save the hospital to the database
await newHospital.save();
res.status(201).send('Hospital signed up successfully');
} catch (error) {
res.status(500).send('Error signing up hospital: ' + error.message);
}
});
app.post('/login-hospital', async (req, res) => {
try {
const { phoneNumber, password } = req.body;
// Find the hospital by phoneNumber
const hospital = await Hospital.findOne({ phoneNumber });
if (!hospital) {
return res.status(401).send('Invalid phone number or password');
}
// Compare the provided password with the hashed password
const isMatch = await hospital.comparePassword(password);
if (!isMatch) {
return res.status(401).send('Invalid phone number or password');
}
// Respond with a success message or token
res.status(200).send('Login successful');
} catch (error) {
res.status(500).send('Error logging in hospital: ' + error.message);
}
});
app.get("/", (req,res)=>{
res.send("i am root")
})
app.listen(8080,()=>{
console.log("server is listening on port 8080")
})