-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
130 lines (104 loc) · 2.24 KB
/
server.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
var express = require("express");
var app = express();
var mongoose = require("mongoose");
var Contact = require("./models/contact")
var bodyParser = require("body-parser");
mongoose.connect("mongodb://localhost/contactlist",function(){
console.log("successfully connected to the mongodb")
})
var PORT = process.env.PORT || 3000;
/*app.use(express.static(__dirname+"/public"))
app.get("/",function(req,res){
res.send("helloajkajkl hyd");
})
app.listen(PORT,function(){
console.log("server is listening at "+ PORT);
})*/
app.use(express.static(__dirname+"/public"))
app.use(bodyParser.json());
app.get("/contactList",function(req,res){
Contact.getContacts(function(err,data){
if(err){
throw err;
}
res.json(data);
})
app.post("/contactList",function(req,res){
var body = req.body; // will fetch body details
Contact.addContact(body,function(err,data){
if(err){
throw err;
}
res.json(data);
})
})
app.get("/contactList/:id",function(req,res){
var id = req.params.id
Contact.getContactById(id,function(err,data){
if(err){
throw err;
}
res.json(data);
})
})
/*app.put("/contactList/:id",function(req,res){
var id= req.params.id;
var body = req.body;
console.log(body.name);
Contact.updateContact(id,body,function(err,data){
if(err){
throw err;
}
res.json(data);
})
})
*/
app.put("/contactList",function(req,res){
var id= req.body._id;
var body = req.body;
console.log(id);
Contact.updateContact(id,body,function(err,data){
if(err){
throw err;
}
res.json(data);
})
})
app.delete("/contactList/:id",function(req,res){
var id = req.params.id
Contact.deleteContact(id,function(err,data){
if(err){
throw err;
}
res.json(data);
})
})
/*app.get("/employeeList",function(req,res){
Contact.getContacts(function(err,data){
if(err){
throw err;
}
res.json(data);
})
})
*/
/*var person1 ={
name: "scott",
email:"[email protected]",
mobile:"(989) 1111-1111"
var person2 ={
name: "tharun",
email:"[email protected]",
mobile:"(989) 22222-1111"
}
var person3 ={
name: "arun",
email:"[email protected]",
mobile:"(989) 3333-1111"
}*/
/*var contactList = [person1,person2,person3];
res.send(contactList)*/
})
app.listen(PORT,function(){
console.log("server is listening at "+ PORT);
})