-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
181 lines (154 loc) · 4.13 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
180
181
const express = require("express");
const ejs = require("ejs");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");
const saltRounds = 10;
const multer = require("multer");
const app = express();
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "./uploads");
},
filename: (req, file, cb) => {
cb(null, `${Date.now()}-${file.originalname}`);
},
});
const upload = multer({ storage });
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
mongoose.connect("mongodb://127.0.0.1:27017/medicalDB", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Define the user schema
const userSchema = new mongoose.Schema({
email: String,
password: String,
secret: String,
});
// Define document schema
const documentSchema = new mongoose.Schema({
name: String,
fileName: {
type: String,
required: true,
},
documentPath: {
type: String,
required: true,
},
});
// Create the User model
const User = mongoose.model("User", userSchema);
// Create the Document model
const Document = mongoose.model("Document", documentSchema);
// Render the register page
app.get("/", (req, res) => {
res.render("login.ejs");
});
app.get("/login", (req, res) => {
res.render("login.ejs");
});
app.get("/register", (req, res) => {
res.render("register.ejs");
});
app.get("/upload", (req, res) => {
res.render("uploads.ejs");
});
app.get("/about", (req, res) => {
res.render("about.ejs");
});
app.get("/home", (req, res) => {
res.render("home.ejs");
});
app.get("/myfiles", (req, res) => {
// Fetch the user's documents from the database using promises
Document.find({})
.then((documents) => {
res.render("myFiles.ejs", { documents });
})
.catch((err) => {
console.log(err);
res.status(500).send("An error occurred while fetching documents.");
});
});
app.get("/download/:fileName", (req, res) => {
const fileName = req.params.fileName;
const filePath = `./uploads/${fileName}`;
res.download(filePath, (err) => {
if (err) {
console.error(err);
res.status(500).send("An error occurred while downloading the file.");
}
});
});
// Handle user registration
app.post("/register", (req, res) => {
// Hash the password using bcrypt
bcrypt.hash(req.body.password, saltRounds, (err, hash) => {
if (err) {
console.error(err);
res.status(500).send("An error occurred while registering.");
} else {
// Create a new user with the hashed password
const newUser = new User({
email: req.body.email,
password: hash,
});
// Save the new user to the database
newUser
.save()
.then(function () {
res.render("home.ejs");
})
.catch(function (err) {
console.log(err);
res.status(500).send("An error occurred while registering.");
});
}
});
});
app.post("/login", (req, res) => {
const username = req.body.email;
const password = req.body.password;
User.findOne({ email: username })
.then(function (foundUser) {
if (foundUser) {
bcrypt.compare(password, foundUser.password, function (err, result) {
if (result === true) {
res.render("home.ejs");
} else {
// Display a client-side alert
res.send(
"<script>alert('Incorrect password'); window.location='/login'</script>"
);
}
});
}
})
.catch(function (err) {
console.log(err);
});
});
app.post("/upload", upload.single("document"), (req, res) => {
if (!req.file) {
res.send(
"<script>alert('No File Uploaded'); window.location='/upload'</script>"
);
}
const newDocument = new Document({
name: req.body.name,
fileName: req.file.filename,
documentPath: req.file.path,
});
newDocument.save();
res.send(
"<script>alert('File Uploaded Successfully'); window.location='/upload'</script>"
);
});
// Start the server
app.listen(3000, () => {
console.log("Server is running on port 3000");
});