-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
164 lines (150 loc) · 4.89 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
// IMPORTS/REQUIREMENTS
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const ejs = require('ejs');
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const saltRounds = 10;
const app = express();
// MIDDLEWARE
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
// DATABASE
mongoose.connect("mongodb://localhost:27017/notesDB");
const notesSchema = new mongoose.Schema({
noteID: String, // Username
topic: String,
content: String
});
const userSchema = new mongoose.Schema({
username: {
type: String,
required: true
},
password: {
type: String,
required: function () {
return this.username ? true : false
}
}
});
const User = mongoose.model("User", userSchema);
const Note = mongoose.model("Note", notesSchema);
// REQUEST
app.get("/", (req, res) => {
res.render("home", {
success: ''
});
});
app.get("/register", (req, res) => {
res.render("register", {
sameUsername: ''
});
});
app.get("/login", (req, res) => {
res.render("login", {
loginStatus: ''
});
});
app.post("/register", (req, res) => {
bcrypt.hash(req.body.password, saltRounds, (hashError, hash)=>{
User.findOne({ username: req.body.username }, (findError, foundUser) => {
if (findError) {
console.log(findError);
} else {
if (foundUser) {
res.render("register", {
sameUsername: "This username already exist. Try another or contact the owner if there is any problem."
});
} else {
const newUser = new User({
username: req.body.username,
password: hash,
});
newUser.save((saveError) => {
if (saveError) {
console.log(saveError);
} else {
res.render("home", {
success: "Successfully registered."
});
}
});
}
}
});
});
});
app.post("/login", (req, res) => {
const enteredUsername = req.body.username;
const enteredPassword = req.body.password;
User.findOne({ username: enteredUsername }, (userFoundError, foundUser) => {
if (userFoundError) {
console.log(userFoundError);
res.render("login", {
loginStatus: "There was some error.😐 Try again!"
});
} else {
if (foundUser) {
bcrypt.compare(enteredPassword, foundUser.password, (compareError, result)=>{
if(result === true){
Note.find({noteID: enteredUsername}, (noteFoundError, foundNotes) => {
if (noteFoundError) {
console.log(noteFoundError);
} else {
res.render("notes", {
username: req.body.username,
notes: foundNotes
});
}
});
} else {
res.render("login", {
loginStatus: "Wrong Password.🤨 Try Again."
});
}
});
} else {
res.render("login", {
loginStatus: "No such user found.😕"
});
}
}
});
});
app.post("/notes", (req, res) => {
let newNote = new Note({
noteID: req.body.username,
topic: req.body.topic,
content: req.body.content
});
newNote.save((saveError) => {
if (saveError) {
console.log(saveError);
} else{
User.find({username: req.body.username}, (err, foundUser)=>{
if(err){
console.log(err);
}else{
Note.find({noteID: req.body.username}, (noteFindError, foundNotes)=>{
if (noteFindError) {
console.log(noteFindError);
} else {
console.log(foundNotes);
res.render("notes", {
username: req.body.username,
notes: foundNotes
});
}
});
}
});
}
});
});
// SERVER CONNECTION
app.listen(3000, () => {
console.log("Server is running on port: http://localhost:3000/");
});