-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
120 lines (100 loc) · 2.58 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
var express = require("express");
var app = express();
var mongojs = require("mongojs");
var db = mongojs("contactlist", ["contactlist"]);
var bodyParser = require("body-parser");
// app.get("/", function(req, res){
// res.send("Hello world!!");
// });
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());
app.get("/contactlist", function(req, res){
console.log("I received a GET request.");
db.contactlist.find(function (err, docs){
console.log(docs);
res.json(docs);
});
// person1 = {
// name : "Abc",
// email: "[email protected]",
// number: "123"
// };
// person2 = {
// name : "Def",
// email: "[email protected]",
// number: "456"
// };
// person3 = {
// name : "Ghi",
// email: "[email protected]",
// number: "789"
// };
// var contactlist = [person1, person2, person3];
// res.json(contactlist);
});
app.post('/contactlist', function(req, res){
console.log(req.body);
db.contactlist.insert(req.body, function (err, docs){
//console.log(docs);
res.json(docs);
sendMail(req.body.email);
});
});
app.delete('/contactlist/:id', function(req, res){
var id = req.params.id;
console.log(id);
db.contactlist.remove({_id:mongojs.ObjectId(id)}, function (err, docs){
//console.log(docs);
res.json(docs);
});
});
app.get("/contactlist/:id", function(req, res){
var id = req.params.id;
console.log("Edit id @server :" + id);
db.contactlist.findOne({_id:mongojs.ObjectId(id)}, function (err, docs){
console.log(docs);
res.json(docs);
});
});
app.put("/contactlist/:id", function(req, res){
var id = req.params.id;
console.log("Edit id @server :" + id);
db.contactlist.findAndModify(
{
query:{_id:mongojs.ObjectId(id)},
update:{$set:{name:req.body.name, email:req.body.email, number:req.body.number}},
new:true
},
function (err, docs){
console.log(docs);
res.json(docs);
});
});
var sendMail = function(email){
var mailer = require("nodemailer");
// Use Smtp Protocol to send Email
var smtpTransport = mailer.createTransport("SMTP",{
service: "Gmail",
auth: {
user: "[email protected]",
pass: "ext123!@#"
}
});
var mail = {
from: "Sujit Singh <[email protected]>",
to: email,
subject: "Welcome to MEAN app.",
text: "Hope to have a nice relation with you.",
html: "<b>Hope to have a nice relation with you.</b>"
}
smtpTransport.sendMail(mail, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
smtpTransport.close();
});
}
app.listen(3000);
console.log("Server listening on port 3000.");