-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
271 lines (225 loc) · 5.83 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
'use strict';
//TODO BONUS: make eslint happy!
// require the packages we need
var Hapi = require('hapi');
var server = new Hapi.Server();
var bunyan = require('bunyan');
var logger = bunyan.createLogger({name: 'the-ninja-log'});
var path = require('path');
//TODO require the database file and assign it to variable called "db"
var db = require('./database');
var Joi = require('joi');
var Boom = require('boom');
var PORT = 8080;
var HOST = 'localhost';
/**
* TODO: it looks like we forgot to install Inert
* install it and require it!
*
* https://nodejs.org/api/modules.html
* https://docs.npmjs.com/cli/install
*
*/
var Inert = require('inert');
server.connection({
host: HOST,
port: PORT
});
server.register([
Inert
],
function(err){
if(err){
//TODO BONUS errors should be logged
}
});
/*
SUPER BONUS: use socket.io
var io = require('socket.io').listen(server.listener);
socket.emit('connected', { message: 'Welcome to realtime Ninjas' });
io.emit('created', ninja);
io.emit('deleted', ninja);
io.emit('changed', ninja);
*/
/**
* TODO: use Inert to serve the client
* Remember: static files folders can be nested so you probably will want to
* use the directory handler
*
* BONUS: use the index property to specify a default file for the
* published directory
*
* https://github.com/hapijs/inert#the-directory-handler
*/
server.route({
method: 'GET',
path: '/{file*}',
handler: {
directory: {
index: 'index.html',
path: path.join(__dirname, 'static/' )
}
}
});
/**
* TODO let's validate them ninjas
*
* this joi object is initialized but (evidently) not correct.
*
* modify the object until client requests are successful
* all properties should be required
*
* be aware that the _id property might or might not be present
*
* BONUS: accept only adult ninjas ( age >= 18 )
* BONUS: name should not contain numbers or special characters
* you will need to use a regex :)
*
*/
/*var ninjaModel = Joi.object({
name: Joi.number(),
age: Joi.boolean(),
gender: Joi.string()
});*/
var ninjaModel = Joi.object({
name: Joi.string().required().regex(/\w+/),
age: Joi.number().required().min(18),
_id: Joi.number().integer().positive()
});
server.route({
method: 'POST',
path: '/api/ninja',
config: { validate: {payload: ninjaModel} },
handler: function (request, reply) {
/**
* TODO persist the ninja into the datastore
* - data is sent via a POST http request
*/
var postData = request.payload;
db.insert(postData, function(err, result){
if(err){
logger.error(err);
return reply(Boom.badImplementation(err));
}
console.log(result);
// and now?
// NOTE: the client expects an object but here we get an array
reply(result[0]);
});
}
});
/**
* TODO: now it's up to you to write a whole route that gets
* all the ninjas stored in the datasource: the db function used is
*
* db.find(function(err, res){ --- }
*
*/
server.route({
method: 'GET',
path: '/api/ninja',
handler: function (request, reply) {
/**
* TODO persist the ninja into the datastore
* - data is in the
*/
db.find({}, function(err, result){
if(err){
logger.error(err);
return reply(Boom.badImplementation(err));
}
// and now?
result.toArray(function(err, res){
reply(res);
});
});
}
});
/**
* TODO we need an api path like this
* /api/ninja/123 where 123 is the id used for the database operations.
*
* http://hapijs.com/tutorials/routing
*
* BONUS - validate the id, it should be a positive integer, and of course
* it should be required
*/
var pathWithId = '/api/ninja/{_id}';
//get a single ninja
server.route({
method: 'GET',
path: pathWithId,
config: {
validate: { params: {_id: Joi.number().integer().positive() }}
},
handler: function(request, reply){
// TODO write the whole handler - the db function used the get a single
// ninja is: db.findOne({_id: [id]), function(err, result){ ... }
// http://hapijs.com/tutorials/routing
//
db.findOne({ _id: request.params._id }, function(err, result){
if(err){
logger.error(err);
return reply(Boom.badImplementation(err));
}
reply(result);
});
}
});
//update a ninja
// TODO - write the whole route!
// it should answer to POST /api/ninja/123 (where 123 is the id)
// the id should be validated as always
// to update an existing object in the database:
// db.update({_id: [_id], [object with new values], function(err, res){...}
server.route({
method: 'POST',
path: pathWithId,
config: {
validate: {
payload: ninjaModel,
params: {_id: Joi.number().integer().positive().required() }
},
},
handler: function(request, reply){
db.update({ _id: request.params._id }, request.payload,
function(err, result){
if(err){
logger.error(err);
return reply(Boom.badImplementation(err));
}
reply(result);
}
);
}
});
// delete a ninja
// TODO - write the whole route! - you should be an expert now
// it should answer to DELETE /api/ninja/123 (where 123 is the id)
// the id should be validated as always
// to delete an existing object in the database:
// db.remove({_id: [_id], function(err, res){...}
server.route({
method: 'DELETE',
path: pathWithId,
config: {
validate: {
params: {_id: Joi.number().integer().positive().required() }
}
},
handler: function(request, reply){
db.remove({ _id: request.params._id },
function(err, result){
if(err){
logger.error(err);
return reply(Boom.badImplementation(err));
}
reply(result);
}
);
}
});
server.start(function(){
console.log('I live again - on '+ HOST +':'+ PORT);
//TODO BONUS - log the fact that the app has started
});