-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (72 loc) · 1.92 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
const express = require('express');
const path = require('path');
const port = 9000;
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());
app.use(express.static('assets'));
var contactList = [
{
name: "Aalok",
phone: "111423423"
},
{
name: "Tony Stark",
phone: "1234567890"
},
{
name: "Coding Ninjas",
phone: "12131321321"
}
]
app.get('/practice', function(req, res){
return res.render('practice', {
title: "Let us play with ejs"
});
});
app.get('/', function(req, res){
Contact.find({} , function(err , Contacts){
if(err){
console.log("error fetching contacts in db");
}
return res.render('home',{
title: "Contact List",
contact_list: Contacts
});
});
});
app.post('/create-contact', function(req, res){
// contactList.push(req.body);
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("********" , newContact);
return res.redirect('back');
});
// return res.redirect('/');
});
app.listen(port, function(err){
if (err) {
console.log("Error in running the server", err);
}
console.log('Yup!My Server is running on Port', port);
})
app.get('/delete-contact/', function(req, res){
let id = req.query.id;
//find the contact in the db using id and delete it
Contact.findByIdAndDelete(id , function(err){
if(err){
console.log("error in deleting an object from database");
return;
}
return res.redirect('back');
})
});