-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (54 loc) · 1.45 KB
/
Copy pathindex.js
File metadata and controls
68 lines (54 loc) · 1.45 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
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'));
var contactList = [
{
name:"Aquib",
phone:"7754945402"
},
]>
app.get('/',function(req,res){
Contact.find({},function(err,Contacts){
if(err){console.log('Error in fetching contacts from db');
return;}
return res.render('home', {
title: "My Contacts list",
contact_list: Contacts
});
});
});
app.get('/practice',function(req,res){
return res.render('practice', {
title:"Let us play with ejs"
});
});
app.post('/create-contact', function(req,res){
// contactList.push({
// name: req.body.name,
// phone: req.body.phone
// });
Contact.create({
name: req.body.name,
phone: req.body.phone
},function(err,newContact){
if(err){console.log('error in creating contact');
return;}
console.log('*******',newContact);
return res.redirect('back');
});
});
// app.get('/delete-contact/:phone', function(req,res){
// console.log(req.params);
// let phone =req.params.phone;
// })
app.listen(port,function(err){
if(err){console.log('Error in running the server',err);}
console.log('yup my server in port',port);
})