-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
154 lines (115 loc) · 3.1 KB
/
index.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
/*
* Copyright (c) 2020
* All rights reserved.
*/
const express = require("express");
const path = require("path");
const port = 8000;
// require mongodb database
const db = require('./config/mongoose');
const Contact = require('./models/contact');
const app = express();
// set template Engine
app.set("view engine", "ejs");
// Join the path from views
app.set("views", path.join(__dirname, "views"));
// middleware function
app.use(express.urlencoded());
// Adding Static Files
app.use(express.static('assets'))
// middleware-1
// app.use(function (req,res,next) {
// console.log('Middleware-1 called');
// next();
// })
// middleware-2
// app.use(function (req, res, next) {
// console.log("Middleware-2 called");
// next();
// });
var contactList = [
{
name:"Ayush",
phone:9554625810
},
{
name:"Sushant",
phone:9362922462
},
{
name:"Karan",
phone:9364535662
},
]
app.get("/", function (req, res) {
// console.log(__dirname)
// res.send("<h1>Cool It's Works</h1>");
Contact.find({},function(err,contacts){
if(err){
console.log('Error in Fetching From Db:')
return;
}
return res.render('home', {
titleName: "Contact List",
contact_List: contacts,
});
});
});
// app.get("/practice", function (req, res) {
// return res.render("practice", {
// titleName: "Let us play with ejs",
// });
// });
app.post("/create-contact", function (req, res) {
// contactList.push({
// name: req.body.name,
// phone: req.body.phone
// });
// contactList.push(req.body);
// push into the database
Contact.create({
name: req.body.name,
phone: req.body.phone
}, function(err, newContact){
if(err){
console.log('Error in Creating a Contact!!');
return;
}
console.log('Hurray!!', newContact);
res.redirect('back');
});
// return res.redirect('back');
});
// deleting contact
app.get('/delete-contact/',function(req,res){
console.log(req.query);
// get the query from url
// let phone = req.query.phone;
// get the id fro the database
let id = req.query.id;
// find the index
// let contactIndex = contactList.findIndex(contact => contact.phone == phone);
// if the index is not found.
// if(contactIndex != -1){
// contactList.splice(contactIndex,1);
// }
// find the contact in the database using id and delete
Contact.findByIdAndDelete(id,function(err,){
if(err){
console.log('Error In deleting the object from the database');
return ;
}
return res.redirect("back");
});
});
// app.get('/about',function (req,res) {
// res.send("<h1>I am using Express.js for making Basic Contact List App");
// });
app.listen(port, function (error) {
if (error) {
console.log("Error in running the server", error);
}
else{
console.log("My Express server are running on port", port);
}
});