-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
96 lines (85 loc) · 2.1 KB
/
app.js
File metadata and controls
96 lines (85 loc) · 2.1 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
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
var express = require('express');
app = express();
mongojs = require('mongojs');
db = mongojs('contact',['contactlist']);
bodyParser = require('body-parser');
//inisialisasi app
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());
app.put('/contact/:id',function(req,res){
var id = req.params.id;
db.contactlist.update(
{_id:mongojs.ObjectId(id)},
{
$set:{
name:req.body.name,
email:req.body.email,
number:req.body.number
}
},
function(err,record){
if (err) throw err;
res.json(record);
})
})
app.get('/contact/:id',function(req,res){
var id = req.params.id;
console.log(id);
db.contactlist.findOne({_id:mongojs.ObjectId(id)},function(err,record){
if(err) throw err;
res.json(record);
})
})
app.delete('/contact/:id',function(req,res){
var id = req.params.id;
console.log(id);
db.contactlist.remove({_id:mongojs.ObjectId(id)},function(err,record){
if(err) throw err;
res.json(record);
});
})
app.post('/contact',function(req,res){
console.log(req.body);
db.contactlist.insert(req.body,function(err,record){
if(err) throw err;
res.json(record);
});
});
app.get('/contact',function(req,res){
//console.log('I received a GET request');
db.contactlist.find(function(err,record){
if(err) throw err;
res.json(record);
});
/*persons =
[
{
name : 'Harmain',
email : 'ahmad.hamain@ymail.com',
number : '08766666666'
},
{
name : 'Ahmad Harmain',
email : 'mail.ahmad.hamain@gmail.com',
number : '08755555555'
},
];
res.json(persons);
*/
})
/*app.get('/',function(req,res){
res.send('Hello mas bro');
});*/
//create sever
app.listen(2016,function(){
console.log('Server is running on port 2016');
});
/*
inisialisasi pembuatan server dan penentuan port.
dalam kasus ini port diset 2016 jika terpakai maka sistem otomatis
akan menggenerate port yang baru atau yg tidak terpakai di sistem
express().set('port',process.env.PORT || 2070);
var server = express().listen(express().get('port'),function(){
console.log("Server is running on "+server.address().port);
});
*/