-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (59 loc) · 1.87 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
const express = require("express");
const path = require("path");
const cors = require("cors");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const withAuth = require("./middlewares/auth");
require("./db/mongoose");
const user = require("./routes/user");
const hospital = require("./routes/hospital");
const review = require("./routes/review");
const booking = require("./routes/booking");
const Hospital = require("./models/Hospital");
const User = require("./models/User");
const Review = require("./models/Review");
const app = express();
app.use(express.json());
app.use(cors());
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.get("/", function(req, res) {
res.sendFile(path.join(__dirname, "./client/public", "index.html"));
});
app.use("/api/user", user);
app.use("/api/hospital", hospital);
app.use("/review", review);
app.use("/api/booking", booking);
app.get("/api/checkToken", withAuth, function(req, res) {
const { email } = req;
User.findOne({ email }, function(err, user) {
if (err) {
console.error(err);
res.status(500).json({
error: "Internal error please try again"
});
} else if (!user) {
Hospital.findOne({ email }, (err, hospital) => {
if (err) {
console.error(err);
res.status(500).json({
error: "Internal error please try again"
});
} else if (!hospital) {
res.status(401).json({
message: "Authenticate first"
});
} else if (hospital) {
res.json({ hospital, isHospital: true });
}
});
} else if (user) {
res.json({ user, isHospital: false });
}
});
});
app.get("/api/", withAuth, function(req, res) {
res.send("Welcome!");
});
const port = 5000;
app.listen(port, () => console.log(`Example app listening on port ${port}!`));