-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
108 lines (97 loc) · 2.79 KB
/
Copy pathindex.js
File metadata and controls
108 lines (97 loc) · 2.79 KB
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
const express = require("express")
const bodyParser = require("body-parser")
const mongoose = require("mongoose")
const bcrypt = require("bcryptjs")
const saltround = 10
var enteredText = ""
var regEmail = ""
var regPassword = ""
var userId=""
var userName=""
var uName=""
const app = express()
app.use(bodyParser.urlencoded({extended: true}))
app.use(express.static(__dirname))
app.set("view engine", "ejs")
const notesSchema = new mongoose.Schema({
text: String,
user: String
})
const Note = mongoose.model("note", notesSchema)
const logSchema = new mongoose.Schema({
email: String,
password: String,
userName: String
})
const Log = mongoose.model("log", logSchema)
app.get("/", function(req,res){
res.render("home")
})
app.post("/", function(req,res){
Log.findOne({email: req.body.lemail}, function(err, foundemail){
if(err){
console.log(err)
}else{
if(foundemail){
bcrypt.compare(req.body.lpassword, foundemail.password, function(err, result){
if(result==true){
userName=foundemail.email
userId=foundemail.id
res.redirect("/notes")
}else{
res.redirect("/")
}
})
}else{
res.redirect("/")
}
}
})
})
app.get("/register", function(req,res){
res.render("register")
})
app.post("/register", function(req,res){
bcrypt.hash(req.body.rpassword, saltround, function(err, hash){
regEmail = req.body.remail
regPassword = hash
uName = req.body.uName
const log = new Log({
email: regEmail,
password: regPassword,
userName: uName
})
log.save()
userName=log.email
userId=log.id
res.redirect("/notes")
})
})
app.get("/notes", function(req,res){
Note.find({}, function(err, textContent){
res.render("index", {text: textContent, userId: userId, uName: uName})
})
})
app.post("/notes", function(req,res){
enteredText = req.body.enteredText
const note = new Note({
text: enteredText,
})
note.save()
note.user=userId
res.redirect("/notes")
})
app.post("/delete", function(req,res){
const del = req.body.btn
Note.findByIdAndDelete(del, function(err){
res.redirect("/notes")
})
})
mongoose.connect("mongodb+srv://9nimbu9:1234567890@cluster0.fq4ljvz.mongodb.net/keep-notes")
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function() {
console.log("Server started succesfully");
});