-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
244 lines (220 loc) · 6.48 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Include Hapi package
var Hapi = require('hapi');
// Include Joi package to validate request params and payload.
var Joi = require('joi');
// Create Server Object
var server = new Hapi.Server();
// Include Mongoose ORM to connect with database
var mongoose = require('mongoose');
// Making connection with `restdemo` database in your local machine
mongoose.connect('mongodb://localhost/restdemo');
// Importing `user` model from `models/user.js` file
var UserModel = require('./models/user');
// Define PORT number You can change it if you want
server.connection({port: 7002});
// Register Swagger Plugin ( Use for documentation and testing purpose )
server.register({
register: require('hapi-swagger'),
options: {
apiVersion: "0.0.1"
}
}, function (err) {
if (err) {
server.log(['error'], 'hapi-swagger load error: ' + err)
} else {
server.log(['start'], 'hapi-swagger interface loaded')
}
});
// Register Good Plugin ( Use to log API url's hit on serer )
server.register({
register: require('good'),
options: {
opsInterval: 1000,
reporters: [{
reporter: require('good-console'),
events: {log: '*', response: '*'}
}]
}
}, function (err) {
if (err) {
console.error(err);
}
});
// Fetching all users data
server.route({
method: 'GET',
path: '/api/user',
config: {
// Include this API in swagger documentation
tags: ['api'],
description: 'Get All User data',
notes: 'Get All User data'
},
handler: function (request, reply) {
//Fetch all data from mongodb User Collection
UserModel.find({}, function (error, data) {
if (error) {
reply({
statusCode: 503,
message: 'Failed to get data',
data: error
});
} else {
reply({
statusCode: 200,
message: 'User Data Successfully Fetched',
data: data
});
}
});
}
});
server.route({
method: 'GET',
//Getting data for particular user "/api/user/1212313123"
path: '/api/user/{id}',
config: {
tags: ['api'],
description: 'Get specific user data',
notes: 'Get specific user data',
validate: {
// Id is required field
params: {
id: Joi.string().required()
}
}
},
handler: function (request, reply) {
//Finding user for particular userID
UserModel.find({_id: request.params.id}, function (error, data) {
if (error) {
reply({
statusCode: 503,
message: 'Failed to get data',
data: error
});
} else {
if (data.length === 0) {
reply({
statusCode: 200,
message: 'User Not Found',
data: data
});
} else {
reply({
statusCode: 200,
message: 'User Data Successfully Fetched',
data: data
});
}
}
});
}
});
server.route({
method: 'PUT',
path: '/api/user/{id}',
config: {
// Swagger documentation fields tags, description, note
tags: ['api'],
description: 'Update specific user data',
notes: 'Update specific user data',
// Joi api validation
validate: {
params: {
//`id` is required field and can only accept string data
id: Joi.string().required()
},
payload: {
name: Joi.string(),
age: Joi.number()
}
}
},
handler: function (request, reply) {
// `findOneAndUpdate` is a mongoose modal methods to update a particular record.
UserModel.findOneAndUpdate({_id: request.params.id}, request.payload, function (error, data) {
if (error) {
reply({
statusCode: 503,
message: 'Failed to get data',
data: error
});
} else {
reply({
statusCode: 200,
message: 'User Updated Successfully',
data: data
});
}
});
}
});
server.route({
method: 'POST',
path: '/api/user',
config: {
tags: ['api'],
description: 'Save user data',
notes: 'Save user data',
validate: {
payload: {
name: Joi.string().required(),
age: Joi.number().required()
}
}
},
handler: function (request, reply) {
// Create mongodb user object to save it into database
var user = new UserModel(request.payload);
//Call save methods to save data into database and pass callback methods to handle error
user.save(function (error) {
if (error) {
reply({
statusCode: 503,
message: error
});
} else {
reply({
statusCode: 201,
message: 'User Saved Successfully'
});
}
});
}
});
server.route({
method: 'DELETE',
path: '/api/user/{id}',
config: {
tags: ['api'],
description: 'Remove specific user data',
notes: 'Remove specific user data',
validate: {
params: {
id: Joi.string().required()
}
}
},
handler: function (request, reply) {
// `findOneAndRemove` is a mongoose methods to remove a particular record into database.
UserModel.findOneAndRemove({_id: request.params.id}, function (error) {
if (error) {
reply({
statusCode: 503,
message: 'Error in removing User',
data: error
});
} else {
reply({
statusCode: 200,
message: 'User Deleted Successfully'
});
}
});
}
});
// Lets start the server
server.start(function () {
console.log('Server running at:', server.info.uri);
});