forked from Sahiljains/Contact-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (114 loc) · 3.35 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
const express = require('express');
const path = require('path');
const port = 8000;
const db = require("./config/mangoose");
const Contact = require("./config/models/contact");
const app = express();
app.set('view engine','ejs');
app.set('views',path.join(__dirname,'views'));
// added a parser .(url encoded not works with params)
app.use(express.urlencoded());
app.use(express.static('assets'));
app.use(function(req,res,next){
req.myName = "sahil";
console.log("middleware 1 is used");
next();
});
app.use(function(req,res,next){
console.log("My name from Mw2",req.myName);
console.log("middleware 2 is used");
next();
});
var contactList = [
{
name:"sdafs",
phone:"1233333"
},
{
name:"ccc",
phone:"23456"
},
{
name:"cccss",
phone:"1234"
},
]
app.get('/', function (req, res) {
console.log("My name from Mw2",req.myName);
return res.render('home', {
title: "Contact List",
contact_list: contactList
});
});
app.get('/profile',function(req,res){
console.log(__dirname);
res.send("sdfdsfsd");
});
app.get('/dsfd',function(req,res){
Contact.find({},function(err,contacts){
if(err){
console.log("dsf");
return;
}
return res.render('home',{
title: "Contact List",
contact_list: contacts
});
});
});
app.get("/practice",function(req,res){
res.render("practice",
{
title:"lets play with ejs",
});
});
app.post("/contact",function(req, res){
//return res.redirect("/practice");
//PARSER FORM DATA
console.log(req.body);
console.log(req.body.name);
console.log(req.body.person);
// Contact.create({
// name:req.body.name,
// phone:req.body.phone
// },function(err, newContact) {
// if(err){
// console.log("sdf");
// return;
// }
// console.log("*****",newContact);
// return res.redirect('back');
});
// contactList.push({
// name:req.body.name,
// phone:req.body.phone
//});
// Contact.push(req.body);
// return res.redirect('/');
//});//end parser form data
//app.get('/delete-contact',functiton(req,res) {
//console.log(req.params);
//let phone = req.params.phone;
//console.log(req.query);
// let id = req.query.id; // for db
// console.log(id); // for db
// let contactIndex = contactList.findIndex(function (contact) {
// return contact.phone===phone;
// });
// Contact.findByIdAndDelete(id,function(err){ // for db
// if (err){ // for db
// console.log(err); // for db
// return; // for db
// } // for db
// return res.redirect('/'); // for db
// }); // for db
// if(contactIndex!=-1){
// return res.redirect('/');
//}); contactList.splice(contactIndex,1);
// }
app.listen(port,function(err){
if(err){
console.log("error in server");
}
console.log("sdcd:",port);
});