-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (55 loc) · 1.45 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
const express = require('express')
const path = require('path')
const port = 8000;
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'))
app.get('/',function(req,res){
Contact.find({},function(err,contacts){
if(err){
console.log('Error in fetching contacts from db')
return
}
return res.render('contacts',{
title:'Contacts List',
Contact_List:contacts
})
})
})
app.get('/practice',function(req,res){
return res.render('practice',{
title:"Practice Page"
})
})
app.post('/create-contact', function(req,res){
Contact.create({
name:req.body.name,
phone:req.body.phone
},function(err,newcontact){
if(err){
console.log('error in creating a new contact')
return
}
return res.redirect('back')
})
})
app.get('/delete-contact',function(req,res){
let id =req.query.id;
Contact.findByIdAndDelete(id,function(err){
if(err){
console.log('Error in deleting a contact from db');
return;
}
return res.redirect('back')
})
})
app.listen(port,function(err){
if(err){
console.log('Error:',err)
}
console.log('My express is running on port:',port);
})