This repository has been archived by the owner on Jan 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
176 lines (147 loc) · 4.15 KB
/
app.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Koneksi Nodejs dengan MongoDB menggunakan Mongoose
*
* Author By Equan Pr.
* http://equan.me
*
* License : Whatever you want! :D
*/
var express = require("express"),
app = express(),
mongoose = require('mongoose'),
path = require('path'),
engines = require('consolidate');
app.configure(function () {
app.use(express.logger());
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type')
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
})
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname+'/public'));
app.engine('html', engines.handlebars);
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.set('PORT', process.env.PORT || 5000);
app.set('MONGODB_URI', process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL || 'mongodb://localhost/persons');
});
/**
* MongoDB connection
*/
var db = mongoose.createConnection(app.get('MONGODB_URI'));
db.on('connected', function () {
console.log('Connected to MongoDB.');
});
db.on('error', function (err) {
console.error.bind(console, 'Connection to MongoDB error!.');
});
db.on('close', function () {
console.log('Connection to MongoDB closed.');
});
// Schema
var PersonsSchema = new mongoose.Schema({
name: 'string',
username: 'string',
website: 'string',
createdAt: 'date',
updatedAt: 'date'
}),
Persons = db.model('Persons', PersonsSchema);
// Routes
app.get("/", function (req, res) {
res.render('index',{
data: 'Silly RESTful sample app built with Node.js, Express, Mongoose and MongoDB. ' +
'Maybe it\'s useful for beginners ;)'
});
});
// GET /persons
app.get("/persons", function (req, res) {
// Find All
Persons.find(function (err, persons) {
if (err) res.json({error: err})
if(persons)
res.json({persons: persons});
})
});
// POST /persons
app.post("/persons", function(req, res){
/**
* Get data from post
* @type {Persons}
*/
var person = new Persons({
name: req.body.name,
username: req.body.username,
website: req.body.website,
createdAt: new Date(),
updatedAt: new Date()
});
person.save(function (err, person) {
if (err) {
res.send({error:err});
}else {
console.log('Save data: ' + person);
res.json({message: ' save ok'});
}
})
});
// GET /persons/:username
app.get('/persons/:username', function(req, res){
var param_username = req.params.username;
Persons.find({username:param_username}, function(err, person){
if(err) {
res.json({
data:"Error finding person."
});
}else {
res.json({
person: person
});
}
})
});
// PUT /persons/:username
app.put('/persons/:username', function(req, res){
var query = {username: req.params.username},
data_update = {
name : req.body.name,
username: req.params.username,
website: req.body.website,
updatedAt: new Date()
}
Persons.update(query, data_update, {multi:false}, function(err, numberAffected, rawResponse ){
if(err) {
res.json({
error:err
})
}else {
res.json({
numberAffected:numberAffected,
rawResponse: rawResponse
});
}
});
});
// DELETE /persons/:username
app.delete('/persons/:username', function(req, res){
var param_username_del = req.params.username;
Persons.remove({username:param_username_del}, function(err){
if(err){ res.json({
error:err
})
}else {
res.json({message: "delete ok"});
}
});
});
app.listen(app.get('PORT'));
console.log("Server Port: " + app.get('PORT'));