-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (106 loc) · 2.77 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
const express=require('express');
const port=8000;
const path=require('path');
const db=require('./config/mongoose');
const Contact=require('./models/contact');
const app=express();
app.set('view engine','ejs');
app.set('views',path.join(__dirname,'views'));
app.use(express.urlencoded());//middleware
app.use(express.static('assets'));
//middleware1
// app.use(function(req,res,next){
// req.myName='bhoomi';
// console.log('middleware 1 called');
// next();
// })
//middleware2
// app.use(function(req,res,next){
// console.log('my name from mw2',req.myName);
// console.log('middleware 2 called');
// next();
// })
const contactList=[
{
name:'Bhoomi',
phone:1234567890
},
{
name:'Aakash',
phone:43546435664
},
{
name:'Kritika',
phone:1111111111
}
]
app.get('/',function(req,res){
Contact.find({},function(err,contacts){
if(err){
console.log('Error in fetching contact from database')
return;
}
return res.render('home',{
title:'Contacts List',
contact_list:contacts
});
});
})
// app.get('/',function(req,res){
// // console.log(__dirname);
// return res.render('home',{
// title:'Contacts List',
// contact_list:contactList
// });
// res.send('<h1>yayy it is running</h1>');
// })
app.post('/create-contact',function(req,res){
// return res.redirect('/practice');
// contactList.push({
// name:req.body.name,
// phone:req.body.phone
// });
// contactList.push(req.body);
Contact.create({
name:req.body.name,
phone:req.body.phone
},function(err,newContact){
if(err){
console.log('error not able to add contact');
return;
}
else{
console.log("**********",newContact);
return res.redirect('back')
}
})
// return res.redirect('back');
});
app.get('/practice',function(req,res){
return res.render('practice',{title:'Let us practise'})
})
//for deleting a contact
app.get('/delete-contact',function(req,res){
//get the query from the url
// console.log(req.query);
//get the id from url
let id=req.query.id;
Contact.findByIdAndDelete(id,function(err){
if(err){
console.log('unable to delete');
return;
}
return res.redirect('back');
})
// let contactIndex=Contact.findIndex(contact=>contact.id == id);
// if(contactIndex != -1){
// Contact.splice(contactIndex,1);
// }
// return res.redirect('back');
});
app.listen(port,function(err){
if(err){
console.log("ERROR CAUGHT",err);
}
console.log('Yep,Server is running on port',port);
})